<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
protected void Page_Load(object sender, EventArgs e)
{
string controlName = Request.Params.Get("__EVENTTARGET");
Response.Write("control caused the postback is: " + controlName);
}
The above code works for all web server controls on the form except for Button and ImageButton.
protected void Page_Load(object sender, EventArgs e)
{
string controlName = Request.Params.Get("__EVENTTARGET");
Response.Write("control caused the postback is: " + controlName);
}
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem>Item11</asp:ListItem>
<asp:ListItem>Item12</asp:ListItem>
<asp:ListItem>Item13</asp:ListItem>
<asp:ListItem>Item14</asp:ListItem>
</asp:DropDownList><br />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem>Item11</asp:ListItem>
<asp:ListItem>Item12</asp:ListItem>
<asp:ListItem>Item13</asp:ListItem>
<asp:ListItem>Item14</asp:ListItem>
</asp:DropDownList><br />
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
<asp:ListItem>Item11</asp:ListItem>
<asp:ListItem>Item12</asp:ListItem>
<asp:ListItem>Item13</asp:ListItem>
<asp:ListItem>Item14</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button 1" />
<asp:Button ID="Button2" runat="server" Text="Button 2" />
<asp:Button ID="Button3" runat="server" Text="Button 3" />
<asp:Button ID="Button4" runat="server" Text="Button 4" />
<br />
<br />
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
Text="CheckBox 1" />
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
Text="CheckBox 2" />
<asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack="True"
Text="CheckBox 3" />
</div>
</form>
</body>
public string GetPostBackControl(Page page)
{
string controlName = page.Request.Params.Get("__EVENTTARGET");
if (controlName == string.Empty)
{
//Means, some Button or ImageButton has been clicked.
foreach (string controlItem in page.Request.Form)
{
Control c = Page.FindControl(controlItem);
if (c is Button)
{
controlName = c.ID;
break;
}
}
}
return controlName;
}
protected void Page_Load(object sender, EventArgs e)
{
string control = GetPostBackControl(this.Page);
Response.Write("Control caused postback:" + control);
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Author
Commented:Hi tedbilly,
I have clicked on EDIT ARTICLE to add some introduction for the purpose to provide a short explanation to the article, but I could not see any changes made when I clicked on SUBMIT ARTICLE button. How can I edit the article to add some introduction to the article?
Author
Commented:I think, I have done all the changes asked by reviewers / page editors.
Anything more changes required?
When can I expect the article to be published?
I feel that I don't have any more problems with the article.
Thanks and Regards,
Ashok kumar.