0

第一页

<html >
<head >
</head>
<body>
<form id="form1" runat="server">

<asp:Button ID="btnSubmit" runat="server" 
            OnClick="btnSubmit_Click"            
            Text="Submit to Second Page" />
</div>
</form>
</body>

btnSubmit_Click 事件

Response.redirect("Page2.aspx");

在Page2的Page Load中,如何查找是哪个按钮导致了postback?

4

4 回答 4

1

btnSubmit_Click事件中,您可以传递查询字符串参数并在 Page2 中获取查询字符串参数。

Response.redirect("Page2.aspx?btnName=button1");

在 Page2.aspx 页面的加载事件

protected void Page_Load(object sender, EventArgs e)
{
      string queryString = Request.QueryString["btnName"].ToString();
      //Here perform your action
}
于 2013-06-19T13:09:05.890 回答
0

If you really need the information from which button the post came, you'd implement Cross-Page Posting. It's a good article with examples. I'd prefer that method over using a QueryString (which might be slightly faster to implement).

于 2013-06-19T13:25:22.523 回答
0

有没有尝试过这个解决方案?此处:在回发时,如何检查哪个控件导致 Page_Init 事件中的回发

public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    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 controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            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;
        }
    }

    return control == null ? String.Empty : control.ID;
}
于 2013-09-23T08:07:43.693 回答
0

在 中Page_Load,无法找到是哪个按钮导致的PostBack。在第 2 页的观点中,它甚至不是帖子。这是一个全新的要求。这就是 Response.Redirect 所做的,它指示客户端浏览器执行一个新请求。

如果您真的需要知道,您可以添加一个 URL 参数,并将其作为查询字符串获取,因为 Pankaj Agarwal 节目就是他的答案。

评论后编辑:除了查询字符串,您可以Session在 onClick 中使用类似:

Session["POST-CONTROL"] = "button2"

在 中Page_Load,您将其读作:

var postControl = Session["POST-CONTROL"] != null ? Session["POST-CONTROL"].toString() : "";
于 2013-06-19T13:11:01.557 回答