Saturday, March 24, 2012

Exception when using public attributes of user control containing UpdatePanel

I have a page that utilizes a user control. This control is compiled in using a web deployment project. The project is not set to allow the precompiled site to be updatable.

This user control has a number of attributes that set/access child control properties.

My problem is that within my user control, no child control within the UpdatePanel is instantiated when the page attributes are being processed. When I try to declare attributes for my user control within the .aspx, I get a null reference exception. The generated code that sets these attributes appears to run too early. If I set the attributes programmatically at Page_Init or Page_Load, everything works okay.

On error, the flow of execution is this:

1) Page's Page_Init

2) Exception (attempt to set attribute of null control)

(Not called: Control's Page_PreInit, Control's Page_Init)

I have tried calling EnsureChildControls() for both the page and the user control, but it does not make any difference.

It looks something like this:

ASPX file:

<asp:Content ID="thePage" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server"> <MyControls:theControl id="WebLinksControl" runat="server" CssClass="Content_FullPage" ListTitleText="my text" /></asp:Content>
ASCX file:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional" > <ContentTemplate> <div class="ListTitle"> <asp:Label ID="lblListTitle" runat="server" Text="Replace Me"> </asp:Label> </div></ContentTemplate></asp:UpdatePanel>

The accessor:

/// <summary> /// Get/set the text of the label: List title /// </summary>public string ListTitleText {get {// Retrieve object propertyreturn lblListTitle.Text; }set {// Override null with empty stringif (value ==null)value = String.Empty;// Set object property lblListTitle.Text =value; } }

I'm currently experiencing this problem myself. Has anyone been able to solve this?

Me too. Has anyone been able to resolve this?

Edmund


Hi, I'm having the same issue. Anyone found a solution yet?

As you mentioned, I can get around it by setting the attributes programmatically but this isn't really desirable


Try this solution it worked for me:

Create a private variable to temporarily store the value of the property. Override the render method of the user control to assign the property to the textbox.

Private _maxLengthAsInteger

PublicProperty MaxLength()AsInteger
Get
Return txtValue.MaxLength
EndGet
Set(ByVal valueAsInteger)
_maxLength = value
End Set
EndPropertyOverridesSub Render(ByVal writerAs System.Web.UI.HtmlTextWriter)
txtValue.MaxLength = _maxLength
MyBase.Render(writer)
EndSub

Good luck, Hanan Schwartzberg
--------
Custom programming and web design
http://www.lionsden.co.il

Has a solution been found to this problem?

As for the temporary variable in the property suggestion, wouldn't that have a problem for any property accessed before the render occurred ie the set value is in the temp but the get is pointing to the actual child control. Seems like this causes an out of sync issue with the property values until the render method occurs.

Is there any way to ensure that the child controls exist before a property tries to get set without having to change a usercontrol into a custom control?


I believe you are correct about the problem with loading the temporary values into the controls being in the render method. The solution is, in my example, to move

txtValue.MaxLength = _maxLength

into the Page.Load method.

Hanan

No comments:

Post a Comment