GridViewRow FindControl failing

E

Erik

Good Afternoon,
I am creating some dynamic controls on RowDataBound for a GridView (this
code is at the bottom). The controls are created and rendered properly, but
FindControl is failing when trying to get the value for each control:

For Each r As GridViewRow In gvwResults.Rows
Dim sValue As String = ""
'Cell(1) - ManagerTypeCode
Dim sManagerTypeCode = r.Cells(1).Text
'Cell(2) - ProfileFieldID
Dim sProfileFieldId As String = r.Cells(2).Text
'Cell(3) - control created in gvwResults_RowDataBound
If Not r.Cells(3).FindControl("txtb" & sProfileFieldId) Is Nothing Then
sValue = CType(r.Cells(3).FindControl("txtb" & sProfileFieldId),
TextBox).Text
ElseIf Not r.Cells(3).FindControl("drop" & sProfileFieldId) Is Nothing Then
sValue = CType(r.Cells(3).FindControl("drop" & sProfileFieldId),
DropDownList).SelectedValue
End If
Next

An example for a control's ID when viewing the source is
"ctl00_Content_gvwResults_ctl02_drop1". I have tried this value, and just
"drop1", but cannot find the control.

Any suggestions, help, or insight on why I am unable to find the dynamic
controls is greatly appreciated.

Thank you, Erik


Protected Sub gvwResults_RowDataBound(ByVal sender As Object, ByVal e As
GridViewRowEventArgs) Handles gvwResults.RowDataBound
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
If Not rowView Is Nothing Then
Dim sName As String = rowView("ProfileFieldName").ToString
Dim sListValues As String =
rowView("ProfileFieldListValues").ToString

If e.Row.RowType = DataControlRowType.DataRow Then
Dim sValue As String =
GetContactProfileValue(rowView("ProfileFieldID"))
If sListValues.Length <> 0 Then
Dim splitout = Split(sListValues, ",")
Dim arrl As New ArrayList

For i As Integer = 0 To UBound(splitout)
arrl.Add(splitout(i))
Next

Dim ddl As New DropDownList
ddl.ID = "drop" & rowView("ProfileFieldID").ToString
ddl.Width = 150
ddl.CssClass = "resultsgrid"
ddl.DataSource = arrl
ddl.DataBind()
If sValue.Length <> 0 Then
ddl.Items.FindByText(sValue).Selected = True
End If
e.Row.Cells(3).Controls.Add(ddl)
Else
Dim txtb As New TextBox
txtb.ID = "txtb" & rowView("ProfileFieldID").ToString
txtb.Width = 150
If sValue.Length <> 0 Then
txtb.Text = sValue.Trim
End If
txtb.CssClass = "resultsgrid"
e.Row.Cells(3).Controls.Add(txtb)
End If
End If
End If
End Sub
 
I

Ian Suttle

If I recall, FindControl will only locate the control id specified if
it resides as a direct child of the control you are finding from. For
instance, if you have a control in Page and do Page.FindControl(id)
then it would find it. If you have a Panel (Panel1) with a control and
do Page.FindControl(id) it would not work. You'd need to do
Panel1.FindControl.

Alternatively, you may not know the hierarchy of the controls. In this
case you could recursively loop through the controls on the page to
locate the control you want.

Does that make sense?

Ian Suttle
http://www.iansuttle.com
 
E

Erik

Thank you for the response.

Is there a way to get the dynamic control's value from the GridViewRow cell
if FindControl will not work?
 
I

Ian Suttle

You can definitely do this from some of the gridview events such as
selectedindexchanged. Depending on the event will determine how it's
done. If you were to in fact do this on SelectedIndexChanged it would
like something like (from MS docs):

// Get the selected row from the GridView control.
GridViewRow selectRow = MyGridView.SelectedRow;

or during data binding:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs
e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}

Thanks,
Ian Suttle
http://www.iansuttle.com
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top