开发者

How to Get Source of postback

if (Page.IsPostBack) { //here I need to know which control causes the postback 开发者_JAVA百科 }

Thanks


See this posting

Get control name in Page_Load event which make the post back


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; 

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜