How to check whether UpdatePanel is posting back?

后端 未结 3 889
独厮守ぢ
独厮守ぢ 2021-01-04 18:46

Is there a way to determine if an has performed an Ajax postback similar to how we can use...

if(!Page.IsPostBack) { .         


        
相关标签:
3条回答
  • 2021-01-04 19:09

    I don't know if this will work any better than your solution, but have you tried?:

    if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
    {
        Control ctrl = GetControlThatCausedPostBack(Page);
        if (ctrl is UpdatePanel)
        {
            //handle updatepanel postback
        }
    }
    
    private Control GetControlThatCausedPostBack(Page page)
    {
        //initialize a control and set it to null
        Control ctrl = null;
    
        //get the event target name and find the control
        string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
        if (!String.IsNullOrEmpty(ctrlName))
            ctrl = page.FindControl(ctrlName);
    
        //return the control to the calling method
        return ctrl;
    }
    
    0 讨论(0)
  • 2021-01-04 19:09

    Try out following:

    var controlName = Page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(controlName))
    {
         // Use FindControl(controlName) to see whether 
         // control is of UpdatePanel type
    }
    

    Helpful links:

    • ASP.NET: Recursive FindControl & Extension Methods
    0 讨论(0)
  • 2021-01-04 19:16

    You can check whether the postback was asynchronous and whether it was issued by an update panel looking at these properties:

    ScriptManager.GetCurrent(Page).IsInAsyncPostback
    ScriptManager.GetCurrent(Page).AsyncPostbackSourceElementID
    
    0 讨论(0)
提交回复
热议问题