I ran into a situation where I needed to develop a flexible user control that would work regardless of the control in which it was nested. The control was being developed for a page that utilized a FormView, but I wanted it to be able to work in a GridView
without having to adapt it. The user control would exist side by side with an asp:textbox in the same naming container. Since the normal approach of referencing the control was not an option (repeatedly using 'FindControl' to get a reference to the target
control), I needed a reliable means of always being able to reference the asp:textbox. My solution was to pass the name of the asp:textbox into the control as a property and then use the .NamingContainer property to build the path to the textbox. In the sample
code below, the Javascript function is created dynamically using this approach.
Me.Parent.NamingContainer.ClientId gets the .ClientId of the naming container in which the user control resides. From there, it is combined with the name of the textbox to generate the fully qualified ID of the textbox.
Protected Sub GridViewSelector_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Deal with the regular items here
If e.Row.RowState = DataControlRowState.Normal Then
e.Row.CssClass = "SelectorRow"
e.Row.Attributes.Add("onmouseout", "this.className='SelectorRow';")
e.Row.Attributes.Add("onclick", "setValue('" & Me.ParentPopupControlId & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.DisplayTargetControl & "','" & e.Row.DataItem(Me.DisplaySourceField) & "','" & Me.Parent.NamingContainer.ClientID
& "_" & Me.InputTargetControl & "','" & e.Row.DataItem(Me.InputSourceField) & "')")
End If
'Deal with the alternating items here
If e.Row.RowState = DataControlRowState.Alternate Then
e.Row.CssClass = "SelectorAlternatingRow"
e.Row.Attributes.Add("onmouseout", "this.className='SelectorAlternatingRow';")
e.Row.Attributes.Add("onclick", "setValue('" & Me.ParentPopupControlId & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.DisplayTargetControl & "','" & e.Row.DataItem(Me.DisplaySourceField) & "','" & Me.Parent.NamingContainer.ClientID
& "_" & Me.InputTargetControl & "','" & e.Row.DataItem(Me.InputSourceField) & "')")
End If
dch3
Member
447 Points
638 Posts
Use NamingContainer to Get Client-Side ID of a Control in the Same NamingContainer as a UserContr...
Sep 13, 2010 06:33 PM|LINK
I ran into a situation where I needed to develop a flexible user control that would work regardless of the control in which it was nested. The control was being developed for a page that utilized a FormView, but I wanted it to be able to work in a GridView without having to adapt it. The user control would exist side by side with an asp:textbox in the same naming container. Since the normal approach of referencing the control was not an option (repeatedly using 'FindControl' to get a reference to the target control), I needed a reliable means of always being able to reference the asp:textbox. My solution was to pass the name of the asp:textbox into the control as a property and then use the .NamingContainer property to build the path to the textbox. In the sample code below, the Javascript function is created dynamically using this approach. Me.Parent.NamingContainer.ClientId gets the .ClientId of the naming container in which the user control resides. From there, it is combined with the name of the textbox to generate the fully qualified ID of the textbox.
Protected Sub GridViewSelector_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Deal with the regular items here
If e.Row.RowState = DataControlRowState.Normal Then
e.Row.CssClass = "SelectorRow"
e.Row.Attributes.Add("onmouseout", "this.className='SelectorRow';")
e.Row.Attributes.Add("onclick", "setValue('" & Me.ParentPopupControlId & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.DisplayTargetControl & "','" & e.Row.DataItem(Me.DisplaySourceField) & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.InputTargetControl & "','" & e.Row.DataItem(Me.InputSourceField) & "')")
End If
'Deal with the alternating items here
If e.Row.RowState = DataControlRowState.Alternate Then
e.Row.CssClass = "SelectorAlternatingRow"
e.Row.Attributes.Add("onmouseout", "this.className='SelectorAlternatingRow';")
e.Row.Attributes.Add("onclick", "setValue('" & Me.ParentPopupControlId & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.DisplayTargetControl & "','" & e.Row.DataItem(Me.DisplaySourceField) & "','" & Me.Parent.NamingContainer.ClientID & "_" & Me.InputTargetControl & "','" & e.Row.DataItem(Me.InputSourceField) & "')")
End If
e.Row.Attributes.Add("onmouseover", "this.className='SelectorRowHighlight';")
End If
LabelSelectorTitle.Text = LabelSelectorTitle.Text
End Sub