In this article I will explain with an example, how to find the controls that caused PostBack in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The HTML Markup consists of following controls to find the ID of the control which caused PostBack:
1. Button
2. LinkButton
3. ImageButton
4. RadioButton
5. CheckBox
6. DropDownList
7. CheckBoxList
8. RadioButton
9. TextBox
10. HiddenField
All controls have been assigned with the AutoPostBack property set to true except Button, LinkButton and ImageButton.
Note: When AutoPostBack property set to true, __doPostBack function is called for control which caused PostBack. The __doPostBack function is not visible in source of the page.
 
The Button and ImageButton have been assigned with an OnClientClick event handler as they do not use JavaScript __doPostBack function for triggering PostBack.
HiddenField – For storing ID of the control which caused PostBack.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="SetSource(this.id)" /><br />
<asp:LinkButton ID="lnkSubmit" runat="server">Submit</asp:LinkButton><br />
<asp:ImageButton ID="ibtnSubmit" runat="server" OnClientClick="SetSource(this.id)" AlternateText="Submit" /><br />
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" /><br />
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" /><br />
<asp:DropDownList ID="ddlCompanies" runat="server" AutoPostBack="true">
    <asp:ListItem>ASPSnippets PVT LTD</asp:ListItem>
    <asp:ListItem>ASPSnippets Advertising</asp:ListItem>
</asp:DropDownList>
<asp:CheckBoxList ID="chkCompanies" runat="server" AutoPostBack="true">
    <asp:ListItem>ASPSnippets PVT LTD</asp:ListItem>
    <asp:ListItem>ASPSnippets Advertising</asp:ListItem>
</asp:CheckBoxList>
<asp:RadioButtonList ID="rbtnCompanies" runat="server" AutoPostBack="true">
    <asp:ListItem>ASPSnippets PVT LTD</asp:ListItem>
    <asp:ListItem>ASPSnippets Advertising</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:HiddenField ID="hidSourceID" runat="server" />
 
 
Finding ID of the Button and ImageButton controls
The __doPostBack function is not used by Button and ImageButton controls and hence the following function is used to send the ID of the Button and ImageButton controls.
SetSource
Inside the SetSource function, the ID of the control that caused PostBack is set to the HiddenField.
<script type="text/javascript">
    function SetSource(SourceID) {
        var hidSourceID = document.getElementById("<%=hidSourceID.ClientID%>");
        hidSourceID.value = SourceID;
    }
</script>
 
 
Displaying the ID of the Control which caused PostBack
Inside the Page_Load event handler, a check is performed if the Request.Form collection of the control that caused PostBack is null or not. If it is not then, the ID of that control is fetched.
If it is null then, the HiddenField is checked for ID of the controls i.e. Button or ImageButton that caused PostBack.
Note: For Button and ImageButton OnClientClick event handler is used to call __doPostBack.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    ddlCompanies.ToolTip = "ASPSnippets";
    if (this.IsPostBack)
    {
        string ctrlID = string.Empty;
        if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"] != string.Empty)
        {
            ctrlID = Request.Form["__EVENTTARGET"];
        }
        else
        {
            //Buttons and ImageButtons.
            if (Request.Form[hidSourceID.UniqueID] != null && Request.Form[hidSourceID.UniqueID] != string.Empty)
            {
                ctrlID = Request.Form[hidSourceID.UniqueID];
            }
        }
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Control ID " + ctrlID + " caused postback.')", true);
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    ddlCompanies.ToolTip = "ASPSnippets"
    If Me.IsPostBack Then
        Dim ctrlID As String = String.Empty
        If Request.Form("__EVENTTARGET") IsNot Nothing And Request.Form("__EVENTTARGET") <> String.Empty Then
            ctrlID = Request.Form("__EVENTTARGET")
        Else
            'Buttons and ImageButtons.
            If Request.Form(hidSourceID.UniqueID) IsNot Nothing And Request.Form(hidSourceID.UniqueID) <> String.Empty Then
                ctrlID = Request.Form(hidSourceID.UniqueID)
            End If
        End If
        ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Control ID " & ctrlID & " caused postback.')", True)
    End If
End Sub
 
 
Screenshot
How to find the control that caused PostBack in ASP.Net
 
 
  
Downloads