asp.netasynchronous-postback

ASP.NET Page_PreRender method


I'm trying to set a htmltablecell.innerHTML from code behind on Page_PreRender.

It works fine the first time it's set.

Hereafter I need to set the reference to a new value from a string (I have checked that the string value is changed), but it keeps displaying the old value in the HTML output.

When I debug the htmltablecell.innerHTML, I can also see that the value has changed.

The reason why the value is set from Page_PreRender method, is that I have a User Control which updates the string after the Page_load has fired.


Solution

  • What you desribe can occur, if the post back is asynchronous (fired from a control in an UpdatePanel) when the control being accessed, (the HtmlTableCell in this case) is outside the UpdatePanel.

    Disregard if this is not the case.

    Edit

    You have two options.

    You can setup the control within the UpdatePanel which is initiating the async postback to perform a standard postback. To do this, define a PostBackTrigger within the UpdatePanel, setting the ControlID to the ID of the control you wish to perform the postback.

    (Because this will be performing a full postback, the HtmlTableCell will then be accessible to modify)

    <asp:UpdatePanel ... >
         ...
        <Triggers>
            <asp:PostBackTrigger ControlID="ControlIDToPostBack" />
        </Triggers>
    </asp:UpdatePanel>
    

    Or you could add the HtmlTableCell (and parent rows, table etc) you wish to update to another UpdatePanel setting both of the UpdatePanel's UpdateMode properties to Always.

    This will ensure that the content within both UpdatePanel controls is updated for all postbacks that originate from the page. Which includes asynchronous postbacks.

    <asp:UpdatePanel ... UpdateMode="Always">
    

    Hope this helps.