Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if and which partial postback occurred in pageLoad() with JavaScript in .NET

As I understand it, partial page updates with ASP.NET AJAX cause the JavaScript pageLoad() event handler to be invoked.

My question: Is there a generic way of determining in JavaScript from within the pageLoad() function...

i) If the postback was a partial page update or not.

ii) If so, which panel was updated.

My application uses a combination of .NET UpdatePanels & Telerik RadAjaxPanels. I'm looking for a generic (preferably JavaScript) solution which doesn't require me to specify a unique client-side callback function for each panel, nor set some flag from within each postback event handler to identify itself to the client-side.

like image 401
Bumpy Avatar asked Apr 09 '13 03:04

Bumpy


1 Answers

To determine if the postback was a partial update or not, you can use ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack. Here's an example:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        // get a reference to ScriptManager and check if we have a partial postback
        if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
        {
            // partial (asynchronous) postback occured
            // insert Ajax custom logic here
        }
        else
        {
            // regular full page postback occured
            // custom logic accordingly                
        }
    }
}

And to get the Update Panel that caused the PostBack, you can look into ScriptManager.GetCurrent(Page).UniqueID and analyze it. Here's an example of doing that:

public string GetAsyncPostBackControlID()
{
    string smUniqueId = ScriptManager.GetCurrent(Page).UniqueID;
    string smFieldValue = Request.Form[smUniqueId];

    if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
    {
        return smFieldValue.Split('|')[0];
    }

    return String.Empty;
}

References:

  • http://forums.asp.net/t/1562871.aspx/1
  • Get ASP.NET control which fired a postback within a AJAX UpdatePanel
like image 72
Ian Avatar answered Sep 27 '22 23:09

Ian