How to Identify Postback event in Page_Load

前端 未结 3 761
滥情空心
滥情空心 2020-12-11 06:53

We have some legacy code that needs to identify in the Page_Load which event caused the postback. At the moment this is implemented by checking the Request data like this...

3条回答
  •  温柔的废话
    2020-12-11 07:30

    This should get you the control that caused the postback:

    public static Control GetPostBackControl(Page page)
    {
        Control control = null;
    
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }
    

    Read more about this on this page: http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

提交回复
热议问题