Last post Dec 06, 2011 10:29 PM by dch3
Dec 06, 2011 10:29 PM|dch3|LINK
I recently designed an ASP.NET user control for use on multiple pages. One of the requirements was to provide the user with a means to 'Close' (hide) the control AND to do it in such a way that the user control is 100% portable. Specifically, to have all
of the relevant code that closes the control in the control's markup or in the code behind. The idea is to eliminate the need to add the code to the code behind of the page on which the code resides.
Once the property was created I specified it on the parent page. This was the *only* thing required on the parent page or any page on which the control is used. Since its a part of the markup of the control, its perfectly acceptable for my requirements.
The 'Close' element on the control is a label that exists within a FormView on the control. In the FormView's PreRender event, I added code which obtains the full Client Id of the element on the parent that needed to be closed. This was done by passing the
ParentContainer value into the .FindControl method of the user controls naming container. Once a reference to the object was obtained, it was a matter of simply accessing the .ClientId.
Parent Page Markup (markup displays the user control in the equivalent of a modal DIV, it is not neccessarily portable to other browsers, but then thats one of the luxuries of working in a corporate environment where there is only one offically sanctioned
<asp:Panel id="PanelContactDetail" runat="server" Visible="false">
<div id="DIV2" runat="server" style="position:absolute;width:100%;height:100%;top:111px;left:11px">
<div style="z-index:1000;position:absolute;background-color:#E7E7FF;top:26px;left:98px;border:solid 2px #000000">
<wmi:ContactDetail runat="server" ID="ContactDetail1" ParentContainer="PanelContactDetail" />
User Control (Property)
Public Property ParentContainer() As String
'ClientId of the ParentContainer on the Parent Page, this allows the control to handle the code required to close it with minimum coding on the parent page
Set(ByVal value As String)
_ParentContainer = value
Private _ParentContainer As String
User Control (FormView PreRender Event)
Protected Sub FormViewContactDetail_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim MyNamingContainer As Object = Me.NamingContainer.FindControl(Me.ParentContainer)Dim LabelCloseContactDetail As Label = CType(sender.FindControl("LabelCloseContactDetail"), Label)LabelCloseContactDetail.Attributes.Add("onclick", "document.getElementById(" & Chr(34) & MyNamingContainer.ClientId & Chr(34) & ").style.display=" & Chr(34) & "none" & Chr(34) & ";")
I could have bypassed obtaining a reference to the user control's naming container and substituted
MyNamingContainer.ClientId with Me.NamingContainer & "_" & Me.ParentContainer. However, this would have hardcoded the underscore. While it is unlikely that Microsoft would change the delimiter in the ClientId from an underscore
to another character, anything is possible. By getting a reference to the object and then using the .ClientId property, the code shouldn't require anychanges if the underscore is no longer used.
Keywords: User Control Parent Page Naming Container Access parent page's control from a user control Reference parent page's control from a user control Control on parent page Working with parent page controls building a user control