如何获得回发的来源

时间:2010-02-10 11:26:20

标签: asp.net postback

if(Page.IsPostBack)             {                //这里我需要知道哪个控件导致回发             }

由于

2 个答案:

答案 0 :(得分:7)

答案 1 :(得分:3)

以下是来自链接的代码"标记为答案"(这里只是粘贴代码以便我们节省读者时间):

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; 

}
相关问题