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 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