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

前端 未结 1 1158
独厮守ぢ
独厮守ぢ 2021-01-13 19:02

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 determ

相关标签:
1条回答
  • 2021-01-13 19:33

    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
    0 讨论(0)
提交回复
热议问题