Reputation: 137
First Page
<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 Event
Response.redirect("Page2.aspx");
In the Page Load of Page2, how to find which button causes postback?
Upvotes: 0
Views: 1927
Reputation: 3979
Do have try this solution ? here : 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;
}
Upvotes: 0
Reputation: 2477
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).
Upvotes: 0
Reputation: 4094
In the Page_Load
, it is not possible to find which button caused the PostBack
. In the Point-Of-View of Page 2, it is not even a Post. It is a completely new request. This is what Response.Redirect does, it indicates to the client browser to do a new Request.
If you really need to know, you can add a URL parameter, and get it as a Query String, as Pankaj Agarwal show is his answer.
EDIT after comment: Besides the Query String, you can use a Session
like in the onClick:
Session["POST-CONTROL"] = "button2"
and in the Page_Load
, you read it as:
var postControl = Session["POST-CONTROL"] != null ? Session["POST-CONTROL"].toString() : "";
Upvotes: 0
Reputation: 11309
on btnSubmit_Click
event you can pass Query String Paramerter and in Page2 get query string Parameter.
Response.redirect("Page2.aspx?btnName=button1");
at Load Event of Page2.aspx page
protected void Page_Load(object sender, EventArgs e)
{
string queryString = Request.QueryString["btnName"].ToString();
//Here perform your action
}
Upvotes: 1