How to Get Source of postback

可紊 提交于 2019-12-10 20:23:08

问题


if (Page.IsPostBack) { //here I need to know which control causes the postback }

Thanks


回答1:


See this posting

Get control name in Page_Load event which make the post back




回答2:


Here is the code from link "marked as Answer"( Just pasting code here so that we can save readers time):

private string getPostBackControlName()
 {

    Control control = null;
    //first we will check the "__EVENTTARGET" because if post back made by       the controls
    //which used "_doPostBack" function also available in Request.Form collection.

    string ctrlname = Page.Request.Params["__EVENTTARGET"];
    if (ctrlname != null && ctrlname != String.Empty)
    {
        control = Page.FindControl(ctrlname);
    }

    // if __EVENTTARGET is null, the control is a button type and we need to
    // iterate over the form collection to find it
    else
    {
        string ctrlStr = String.Empty;
        Control c = null;
        foreach (string ctl in Page.Request.Form.AllKeys)
        {            

            c = Page.FindControl(ctl);               
            if (c is System.Web.UI.WebControls.Button ||

                     c is System.Web.UI.WebControls.ImageButton )
            {
                control = c;
                break;
            }
        }
    }

    if (control == null)
        return "";
    else
        return control.ID; 

}


来源:https://stackoverflow.com/questions/2236383/how-to-get-source-of-postback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!