Losing Hope! FindControl of dynamic gridview not working

K

K B

PLEASE be patient and read this. I believe I've actually reached the end
of the Web trying to find the answer!

I have an aspx using a MasterPage with a ContentPlaceholder.

I have a datatable that I use to dynamically populate a gridview - I
have to do this in order to specify which control type to put in one of
the columns. All this works fine. I've run Trace and can clearly see
that the control.id I assigned (lblQuestionID) is there
(UniqueID: ctl00$cphMain$gv1$ctl02$lblQuestionID for the first row).
Yet...on a button click event I try to get the control value...

Dim QuestionID as String= CType(gv1.FindControl("lblQuestionID"),
Label).Text

...returns "Object reference not set to an instance of an object" error.

***********************
This is how I load the gridview (abbreviated version):

Public Sub gv1_RowDataBound(ByVal sender As System.Object, ByVal e As
GridViewRowEventArgs) Handles gv1.RowDataBound

If e.Row.RowType = ListItemType.Item Or e.Row.RowType =
ListItemType.AlternatingItem Then

'put Question ID in first column
Dim lblID As New Label
lblID.ID = "lblQuestionID"
lblID.Text = dgTable(gv1).Rows(e.Row.DataItemIndex)(0)
e.Row.Cells(0).Controls.Add(lblID)
lblID.EnableViewState = True

I have other controls (dropdownlist, text, etc.) that I need to get the
new value of after being changed by the user. I can't get any control
values including the above example.

BTW, I tested the above FindControl syntax on a simple gridview with a
standard SqlDataSource datasource and it worked. My problem is that I
must use the dynamic controls. Also, I compared both gridview items in
Trace and they were identical right down to the UniqueIDs.

I'm begging anyone out there who could suggest a solution.

Kit

P.S. The following used to work on my old and truly missed datagrid:

QuestionID = CType(datagrid1.Rows(i).Cells(0).Controls(0), Label).Text
-- and I've tried variations such as Controls(2), etc.
 
Joined
Jun 30, 2006
Messages
1
Reaction score
0
RE: Losing Hope! FindControl of dynamic gridview not working

Hi,
I had done something like this aswell, and same thing happened to me as well, and the how I fixed this problem was finding the ContentPanel by doing findcontrol then, find next child control and so on.
Try this way, it worked for me, may work for you too...

osamam
 
Joined
Sep 17, 2007
Messages
1
Reaction score
0
RE: Losing Hope! FindControl of dynamic gridview not working

Based on the random numbering that ASP.Net web server assigns to controls that might end up with conflicting ID numbers, one of the best ways that I came to realize could address this problem is by populating a javascript on the server side and registering it with the page. So you end up with a code like:

protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("businessUnit", PopulateIdsScript());
}

private string PopulateIdsScript()
{
string selectBUchckBx = BusinessUnitGridView.UniqueID.Replace('$', '_');
string script = "<script language =\"javascript\">function getBusinessViewId(idStr){ ";
script += "var selectBUchckBx = '" + selectBUchckBx + "'; ";
script += "if(idStr == 'selectBUchckBx'){ return selectBUchckBx; }"; script += "return ''; }</script>";

return script;
}

This way, you will be able to make use of the javascript getBusinessViewId(idStr) from the browser side and getting the ID.

Remember that I used a GridView for my small sample. The code isn't neat but I hope it helps. If it doesn't, find a way of pinging me and I will be on it.


(UniqueID: ctl00$cphMain$gv1$ctl02$lblQuestionID for the first row).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top