确定哪个控件导致PostBack

时间:2013-08-18 01:57:00

标签: c# asp.net

我有这个aspx:

<asp:ImageButton ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>

现在在Page_Load我想确定由PostBack引起的check或不是,所以我使用此代码跟踪了this问题的方法:

if(FindControl(Page.Request.Params.Get("__EVENTTARGET"))!=check)//if not caused by "check"
    //do something

Page.Request.Params.Get("__EVENTTARGET")为空。(我在ImageButton中使用UpdatePanel

我如何达到目标?

4 个答案:

答案 0 :(得分:4)

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        return;
    Control control = null;
    string controlName = Page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = Page.FindControl(controlName);
    }
    else
    {
        string controlId;
        Control foundControl;
        foreach (string ctl in Page.Request.Form)
        {
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = Page.FindControl(controlId);
            }
            else
            {
                foundControl = Page.FindControl(ctl);
            }
            if (!(foundControl is Button || foundControl is ImageButton)) continue;
            control = foundControl;
            break;
        }
    }
    Label1.Text = control.ID; // label1 must be in UpdatePanel
}

答案 1 :(得分:1)

试试这个:

Control ctrl = null;

string target = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(target))
    ctrl = page.FindControl(target);

if(ctrl == check){
     //check is the control that caused postback
}

**更新**

好的,看起来ImageButtons的功能有点不同。用于标记:

<asp:ImageButton onClientClick="setTarget(this.id)" ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>

<asp:HiddenField ID="targetId" runat="server" />

现在创建一个javascript函数,它将使用启动PostBack的字段的ID填充隐藏字段:

function SetSource(id)
{
    var targetId=
    document.getElementById("<%=targetId.ClientID%>");
    targetId.value = id;
}

最后我们在PostBack中查看它:

Control ctrl = null;

if (Request.Form[targetId.UniqueID] != null &&
    Request.Form[targetId.UniqueID] != string.Empty)
{
    ctrl = Page.FindControl(Request.Form[targetId.UniqueID]);
}

if(ctrl == check){
 //check is the control that caused postback
}

参考:http://www.aspsnippets.com/Articles/How-to-find-the-control-that-caused-PostBack-in-ASP.Net.aspx

答案 2 :(得分:1)

由于这是一个更新面板,请尝试通过ScriptManager获取值,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    var updatePanelControlIdThatCausedPostBack = String.Empty;
    var scriptManager = ScriptManager.GetCurrent(Page);

    if (scriptManager != null)
    {
        var smUniqueId = scriptManager.UniqueID;
        var smFieldValue = Request.Form[smUniqueId];

        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
        {
            updatePanelControlIdThatCausedPostBack = smFieldValue.Split('|')[1];
        }
    }

    if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack))
    {
        // Do something with control ID value that caused UpdatePanel postback here
    }
}

答案 3 :(得分:0)

如果某个控件导致PostBack,其ID在请求集合null中将为非Form.Request[controlID]

如果是Image,请求集合中有两个项目,一个用于x坐标,另一个用于y坐标,用于鼠标单击图像的确切位置。

所以:

if(Form.Request["Img1.x"] != null)