第一页
<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的页面加载中,如何找到哪个按钮导致回发?
答案 0 :(得分:1)
在btnSubmit_Click
事件上,您可以传递查询字符串参数,并在第2页中获取查询字符串参数。
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
}
答案 1 :(得分:0)
在Page_Load
中,无法找到导致PostBack
的按钮。在第2页的观点中,即使是帖子也不。这是一个全新的要求。这就是Response.Redirect所做的,它向客户端浏览器指示执行新请求。
如果你真的需要知道,你可以添加一个URL参数,并将其作为查询字符串,因为Pankaj Agarwal节目是他的答案。
评论后编辑:除了查询字符串,您可以在onClick中使用Session
之类:
Session["POST-CONTROL"] = "button2"
并在Page_Load
中,您将其读作:
var postControl = Session["POST-CONTROL"] != null ? Session["POST-CONTROL"].toString() : "";
答案 2 :(得分:0)
如果您确实需要帖子来自哪个按钮的信息,您可以实施Cross-Page Posting。这是一篇带有例子的好文章。我更喜欢使用QueryString的方法(实现起来可能稍快一些)。
答案 3 :(得分:0)
试过这个解决方案吗? 在这里:On postback, how can I check which control cause postback in Page_Init event
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;
}