Loading ...

Refresh UpdatePanel by registering async. post back control in code-behind | CodeAsp.Net

Refresh UpdatePanel by registering async. post back control in code-behind

 /5
0 (0votes)

In the asp.net forums, one developer asked how to do this from client-side where his asp.net LinkButton control is outside the UpdatePanel. I have written simple example on how to achieve this, so I thought it would be good to share it here with you.

We have the following situation:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <%= DateTime.Now %>
            </ContentTemplate>
        </asp:UpdatePanel>        
        <asp:LinkButton ID="MyLinkButton" runat="server" Text="Click me" />
        <br />
        This will refresh only first page load time: <%= DateTime.Now %>
    </div>
    </form>
</body>
</html>


So, we have two places where we print DateTime.Now, in order to follow how update panel refreshes. Once we click on the LinkButton with id='MyLinkButton', the update panel should refresh. Now, if we run the example, we will see that on each LinkButton click, both DateTime.Now are refreshed and show the current date time after postback.

The only thing we should do to make this work is just register the following line in code-behind:

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.MyLinkButton);


So, the LinkButton with id='MyLinkButton' is registered as control that does asynchronous post back. Therefore, if you have multiple update panels, all these will be refreshed when MyLinkButton is clicked, but nothing else which is outside the update panels.

You will achieve pretty much the same if you define

            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="MyLinkButton" EventName="Click" />
            </Triggers>


To any of the update panels. However, if you have this on code-behind, you can make additional dynamic logic to chose wether your control will make normal post back or async. post back.

Note: I have somehow abandoned update panels in the last year and switched more in the jQuery Ajax style, but I felt this might be useful for those still working with Update Panels, since I see still many users ask a lot questions in the forums which are directly linked with Update Panels and Script Manager.

Hope this will be usefull for someone out there.

Regards,
Hajan

Comments (1)

   
Anon
Anon
Thanks was very helpful! Thanks for sharing.
6/21/2016
 · 
by
  • :*
  • :*
  • :
 *

Top Posts