UpdatePanel Extender for ASP.NET AJAX

This control is now available from my CodePlex project @ http://www.codeplex.com/AspNetAjaxControls.

The following question has been raised many times: How can I force an UpdatePanel to postback from client-side JavaScript?  The most common answers I’ve seen include using invisible server-side web controls within an UpdatePanel, and calling __doPostback() with the UpdatePanel’s ClientID as the postback target. 

Both of the above answers definitely work, but they don’t seem to be an elegant solution and they don’t provide the additional functionality that developers typically require.

I set out to provide the following functionality in my solution:

  1. Create an ASP.NET AJAX Extender control to encapsulate the UpdatePanel extensions.
  2. Provide an easy to use client-side function to initiate the postback.
  3. Provide a server-side “Command” event to allow the developers flexibility once the UpdatePanel postback occurs.
  4. Expose client-side events during the lifecycle of the UpdatePanel postback.


The UpdatePanelExtender defines the following properties on both the client and the server:

  1. TargetControlID – Standard ControlID to link the extender with the UpdatePanel.
  2. CommandArgument – An optional argument to be passed during the UpdatePanel postback.
  3. CausesValidation – Whether or not the client-side postback call triggers ASP.NET validation prior to the postback.
  4. ValidationGroup – The ASP.NET validation group to use during validation.
  5. ClientEvents – Collection of events to wire-up during the UpdatePanel postback.


All of the properties can be manipulated on the client using the standard ASP.NET AJAX client-side syntax.

<script type="text/javascript">
    function
update()
    {
        var upExtender = $find('upe1'
);
        upExtender.set_commandArgument(
'Hello World!'
);
        upExtender.postback();
    }
</script>

The $find() syntax for getting a reference to the extender control is optional.  I chose to create a global client-side variable with the same name as the extenders ClientID during the extenders initialization.

The common problem that I faced when I wanted to cause an UpdatePanel postback from client-side script is how to manipulate the controls during my postback when there isn’t any event to handle.

To solve this problem I chose to implement a server-side Command event which fires on the server during the UpdatePanel postback.  The developer now has the opportunity to handle the event and optionally take advantage of any command argument that may have been passed during the postback.

protected void upe_Command(Object sender, CommandEventArgs e)
{
    dateTime.Text =
String.Format("Date: {0}, CommandArgument: {1}", DateTime
.Now, e.CommandArgument);
    dateTime2.Text =
String.Format("Date: {0}, CommandArgument: {1}", DateTime
.Now, e.CommandArgument);
}

The client-side events that are exposed within the extender come directly from the Sys.WebForms.PageRequestManager Class, but only those pertaining to the extenders UpdatePanel will fire.  This allows you to include multiple UpdatePanels, each with their individual extender control and event handlers.

  1. initializeRequest - Raised during the initialization of the asynchronous postback.
  2. beginRequest - Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
  3. pageLoading - Raised after the response from the server to an asynchronous postback is received but before any content on the page is updated.
  4. pageLoaded - Raised after all content on the page is refreshed as the result of either a synchronous or an asynchronous postback.
  5. endRequest - Raised after an asynchronous postback is finished and control has been returned to the browser.


In sample 1, two UpdatePanels have been added to a page each with an UpdatePanelExtender control.

<ae:UpdatePanelExtender ID="upe1" TargetControlID="up1" OnCommand="upe_Command" runat="server">
    <ClientEvents
>
        <BeginRequest EventHandler="beginRequest"
/>
        <InitializeRequest EventHandler="initializeRequest"
/>
        <PageLoading EventHandler="pageLoading"
/>
        <PageLoaded EventHandler="pageLoaded"
/>
        <EndRequest EventHandler="endRequest"
/>
    </ClientEvents
>
</
ae:UpdatePanelExtender>

<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
    <ContentTemplate
>
        <asp:Label ID="dateTime" runat="server"
/>
    </ContentTemplate
>
</
asp:UpdatePanel>

I hope you’ve enjoyed reading about this control and some of its common uses.  Please feel free to contact me if you have any questions or comments.  I have also included the binary and source for this control as a download to this post.

Happy programming. 

 

Tags: , , , ,

AJAX | ASP.NET | Custom Controls

Comments

Milind Yande , on 5/1/2008 7:11:01 PM Said:

Milind Yande

Is this available as part AJAX toolkit 3.5? I want to use it for ASP.NET 2.0. can you redirect me to the source code for it.

wallace , on 5/2/2008 8:58:20 AM Said:

wallace

It's not actually part of the AJAX Control Toolkit.  If you download the source you should be able to easily swap out the references for .NET 2.0 Framework and it should compile without any problems.  If you run into any issues let me know and I can put a 2.0 version up on CodePlex.

Dave , on 5/8/2008 9:33:26 AM Said:

Dave

If you could put up a 2.0 version, that would be great. I think this is an awesome control - especially if I can actually use it!

raghu , on 6/18/2008 9:48:55 AM Said:

raghu

hi,
how can i execute custom javascript within the update panel?
many people suggest abt ScriptManager.RegisterStartupScript, but this seems to be not working for me!

wallace , on 6/18/2008 10:38:16 AM Said:

wallace

When do you want to execute the custom script?  Before the update panel performs the postback? during load? after?

raghu , on 6/18/2008 11:52:07 PM Said:

raghu

during load....
i think i got it.
i was registering the script at the page_load event instead of PreRender event of UpdatePanel,

  protected void updatePanel1_PreRender(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "ShowOther()", true);
    }

thanks for your time
raghu

wallace , on 6/19/2008 8:34:44 AM Said:

wallace

Yes, ScriptManager.RegisterStartupScript() should work fine.  If you are using the UpdatePanelExtender you can also subscribe to the the "pageLoading" or "pageLoaded" events on the client side.

pirobox , on 8/20/2008 2:52:10 PM Said:

pirobox

I've a small issue with your extender. My scenario is this (just a quick draw):
<a onclick="ForceInnerPanelUpdate()" />
<UpdatePanel ID="OuterUP" UpdateMode="Conditional">
  <UpdatePanel ID="InnerUP" UpdatePanel="Conditional">
  </UpdatePanel>
  <UpdatePanelExtender TargetControlID="InnerUP" ID="InnerUPEX" />
</UpdatePanel>

that is: 2 nested (Conditional) update panels, an extender and a link that forces the inner UP update using the "upExtender.postback();" method (Like in your example).
In this case when I click the link both panels are refreshed (not only the inner one as it should happen when using mode "Conditional"). Is there any workaround?

Thanks

wallace , on 1/19/2009 1:22:05 PM Said:

wallace

Your welcome. Let me know if you have any questions regarding its use.
(De nada. Déjame saber si usted tiene alguna pregunta referente a su uso.)

biodiesel forum , on 4/14/2009 5:58:00 PM Said:

biodiesel forum

many people suggest abt ScriptManager.RegisterStartupScript, but this seems to be not working for me!

Bononic , on 4/19/2009 6:43:45 AM Said:

Bononic

Do you have .dll or source for FW2.0 ?? i try to compile but dont work in 2.0 switching fw Frown

thanks!

bononic , on 5/5/2009 2:24:32 PM Said:

bononic

Do you have .dll or source for FW2.0 ?? i try to compile but dont work in 2.0 switching fw Frown

Please sendme nicolasgraupen@gmail.com
thanks!

Mark Kravitz , on 6/3/2009 4:48:39 AM Said:

Mark Kravitz

Getting some issues on the script, will post here if I can not get it fixed.
- - - -
<a href="www.blogher.com/.../Kellie+Holmes">Support Open Source</a>

cory , on 6/18/2009 1:42:13 PM Said:

cory

Having some issues getting the Server side Command to wire up when the update panel and this extender are in a dynamically loaded user control. The postback still occurs, but the Command event handler never gets called. Any ideas how to get that event wired up in the dynamic user control case?

Thanks

LF , on 6/23/2009 6:37:39 AM Said:

LF

Also having problems  getting the Server side Command to wire up when the update panel - any ideas?

club penguin , on 7/6/2009 2:13:05 AM Said:

club penguin

How can i execute custom javascript within the update panel? Many people suggest abt ScriptManager.RegisterStartupScript, but this seems to be not working for me!

Emo Clothes , on 7/14/2009 2:11:12 AM Said:

Emo Clothes

Also having problems getting the Server side Command to wire up when the update panel - any ideas?

KI United States, on 9/6/2010 6:09:52 PM Said:

KI

We have been using this tool in a .NET 3.5 website and works very well. We just upgraded to VS 2010 and this seems to be broke.

sender._postBackSettings.panelID returns null.

Anyone else with the same issue?

gsm amplifier , on 1/13/2012 2:00:28 AM Said:

gsm amplifier

I real;y enjoy to read this. Thanks a lot!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading