AJAX UpdatePanel - "Statefull" Control Update Trigger

by JoeStagner 4/13/2008 7:53:57 PM

I get an obscene amount of email. Since I've been working with the Developer Community at Microsoft for 7 years, my email address has been spread around a bit. I get about 1000 email a day. Very often from developers who what me to write code for them :)

I generally don't have time to do that, but sometimes I get an email from someone why has really tried to solve a problem that should be simple, but whose answer is not always as obvious as the problem would lead you to believe.

This week a Developer emailed me about updating an UpdatePanel.

One of the true strengths of ASP.NET is the ability to take several different approaches to writing applications based on your needs and preferred development style.

Though I think the UpdatePanel control is AWESOME, my personal preference for AJAX style programming leads me to write more client side code and communicate with the server via JavaScript enabled web service calls.

The problem was, the developer was using the UpdatePanel and, due to functionality in the business layer, he needed to prevent the user from click a submit button twice in a row. Meaning, when the user clicks the button that caused the UpdatePanel to update, he needed that buttonto be disabled until the UpdatePanel's refresh was complete.

Before Click.....

Pre-Update

Then, after the click but before the UpdatePanel has completed it's update.....

After_Click

And yes... I know that in a real application one should add some updating indicator.

So, the UpdatePanel definition looks like this.  

   22         <div>

   23             <asp:UpdatePanel ID="UpdatePanel1" runat="server">

   24                 <ContentTemplate>

   25                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

   26                     <br />

   27                     <br />

   28                     <asp:Label ID="Label1" runat="server" Text="Label" Width="622px"></asp:Label><br />

   29                     <br />

   30                 </ContentTemplate>

   31             </asp:UpdatePanel>

   32         </div>

   33         <br />

   34         <input id="SubButton" style="width: 618px" type="button" value="Call UpdatePanel Method" onclick="return SubButton_onclick()" />

   35         <br />

   36         <br />

 

Note that the HTML button control "SubButton" is outside the UpdatePanel and is not defined as a Trigger to the UpdatePanel.

 

In order to turn the Button off and get the UpdatePanel to update, we're going to do it all in JavaScript.

If you use an ASP.NET Button control and disable the Button with an OnClientClick event handler, that code fires first and the postback never occurs.

 

Our JavaScript "SubButton_onclick()" function looks like this.

 

    4 <script type="text/javascript">

    5 <!--

    6 function SubButton_onclick()

    7 {

    8     var prm = Sys.WebForms.PageRequestManager.getInstance();

    9     var mybutton = document.getElementById('SubButton')

   10     mybutton.disabled = true;

   11     prm._doPostBack('UpdatePanel1', '');

   12 }

   13 // -->

   14 </script>

 

Hopefully the code is self explanatory. 

 

The Button is disabled and the UpdatePanel postback is triggered.

 

But.... How do we know when the update is complete so we can re-enable the Button.

 

 

   37 <script type="text/javascript">

   38 <!--

   39 var prm = Sys.WebForms.PageRequestManager.getInstance();

   40 

   41 prm.add_endRequest(EndRequest);

   42 

   43 function EndRequest(sender,args)

   44     {

   45     var mybutton = document.getElementById('SubButton');              

   46     mybutton.disabled = false;

   47     }

   48  -->

   49 </script>

 

Luckily, the PageRequestManager is throughly evented. :) 

 

We just add an "EndRequest" event handler and have it re-enable the Button.

 

Pretty simple after you see the solution :)

 

[ Download the code HERE. ]

 

Currently rated 3.7 by 15 people

  • Currently 3.666666/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

AJAX | ASP.NET | Joe Stagner [Syndicated]

Related posts

Comments

4/14/2008 7:52:22 AM

I've run into this problem before too and that is a very nice solution. Thanks.
When I have dealt with this in the past, it was under a different condition of the submit button finishing its work and navigating away from the current page. To handle that I was using:

<asp:Button ID="submitBtn" runat="server" OnClick="submitBtn_Click" Text="Submit" UseSubmitBehavior="false" OnClientClick="this.disabled=true;this.value='Please Wait';" />

Your solution is much more thorough though.

Have a nice one,
Chris

Chris Nicolich us

4/14/2008 8:26:10 AM

Another approach as a solution to these types of problems can be:

basgun.wordpress.com/.../

Mustafa Basgun us

4/14/2008 11:30:47 AM

The AJAX Control Toolkit also has an UpdatePanel Animation Extender that has come in handy for me for exactly this purpose.

Eric Hoff ca

4/14/2008 1:05:31 PM

I am just wondering, why can't you use the following method in Ajax

Button1.Attributes.Add("onclick", "javascript:" & Button1.ClientID & ".disabled=true;" & Page.ClientScript.GetPostBackEventReference(Button1, "") + ";this.value='Processing your order now...'")

This should disable the button until you are processing something on the click event.

Lakh Gill ca

4/14/2008 2:01:37 PM

Lakh - you're correct, that works fine as long as you don't need to do any client postprocessing to the UpdatePanel update event.

Joe

JoeStagner us

4/16/2008 9:37:41 AM

I really like this solution, but I would like also having a server-side handler for the button.
that's my idea
I added sender button into UP triggers
I added generic handlers for ajax posting
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
I retrieve sender into BeginRequestHandler and I add it into request user context
function BeginRequestHandler(sender,args) {
var elem = args.get_postBackElement();
if(elem && elem.id == MyBtn.id)
{
var request = args.get_request();
request.set_userContext(elem.id);
// custom function to wait ajax response
ShowWaitMasssage();
}
}
I verify into EndRequestHandler if user context match with previous one
function EndRequestHandler(sender,args) {
var response = args.get_response();
var request = response.get_webRequest();
var requestSender = request.get_userContext();
if(requestSender && typeof(requestSender)=='string' && requestSender == MyBtn.id)
HideWaitMasssage();
}
what do you think?

Cristiano Degiorgis it

4/17/2008 4:41:58 AM

Wow...very simple solution for a big problem.

Goinath gb

4/18/2008 2:18:52 PM

Pingback from cs2007.websitemagazine.com

AJAX UpdatePanel - "Statefull" Control Update Trigger - Website Magazine - Website Magazine

cs2007.websitemagazine.com

4/24/2008 8:11:39 AM

I m getting a JS error saying that Sys is undefined.

Pls help.

Fenil Desai in

4/24/2008 10:13:45 AM

Sys is the namespace for Microsoft AJAX.

Anil us

5/23/2008 7:14:53 AM

Hi I am using
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._doPostBack(upPanelTabSelected,"TabSelected:"+selectedValue);

in my javascript to do the update of the update panel, but when i am using this for update, all the session variables in my screen are set to null. Can you suggest me the reason why its happening.
I have proper setting done for session timeout, and also my session variable persists through my postback(s), until and unless i use these lines for postback.

Ashish Jain in

5/23/2008 7:36:26 PM

Sorry - thats not enough information to tell me what might be going wrong. Feel free to contact me directly.

JoeStagner us

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About your host.

Name of author Joe Stagner
?????

E-mail me Send mail

Calendar

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Pages

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in