Sunday, June 13, 2010

How to find the control which caused a postback (Part 1 : Outside the event handler) - Asp.net

This article demonstrate how to find the control which cause a postback of a page outside the event handler. This code snippest cover
  1. Button
  2. Link Button
  3. TextBox
  4. Dropdown List
  5. Check Box
  6. Radio Button
Please note: When you try to find the clicked asp.net button, you have to turn off submit bebaviour to cuase a __doPostBack call
    Example

    <%@ Page Language="C#" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <script runat="server"> 
            protected override void OnLoad(EventArgs e)
            {
                if (this.IsPostBack)
                {
                    string uniqueId = this.Request.Form["__EVENTTARGET"];
                    if (!string.IsNullOrEmpty(uniqueId))
                    {
                        string[] parts = uniqueId.Split("$".ToCharArray());
                        string id = parts[parts.Length - 1];
                        Control ctrl = this.FindControl(id);
                        if (this.pnlPostbackDetails.Visible = ctrl != null)
                            this.lblControl.Text = string.Format(
                                "Type: {0} , Id: {1}", ctrl.GetType().Name, id);
                    }
                }
                base.OnLoad(e);
            }        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Panel runat="server" ID="pnlContainer">
                <asp:Button 
                    runat="server" 
                    ID="btnPostabck" 
                    Text="Postback" 
                    UseSubmitBehavior="false" />
                <asp:LinkButton runat="server" ID="lnkPostback" Text="Postback" />
                <asp:TextBox runat="server" ID="txtName" AutoPostBack="true" />
                <asp:DropDownList runat="server" ID="ddlCountries" AutoPostBack="true">
                    <asp:ListItem>Select</asp:ListItem>
                    <asp:ListItem>SL (Sri Lanka)</asp:ListItem>
                    <asp:ListItem>UK (United Kingdom)</asp:ListItem>
                    <asp:ListItem>US (United States)</asp:ListItem>
                </asp:DropDownList>
                <asp:RadioButton 
                    runat="server" 
                    ID="rbPostback" 
                    Text="Postback" 
                    AutoPostBack="true" />
                <asp:CheckBox 
                    runat="server" 
                    ID="chkPostback" 
                    Text="Postback" 
                    AutoPostBack="true" />
            </asp:Panel>
            <hr />
            <asp:Panel runat="server" ID="pnlPostbackDetails" Visible="false">
                Postback Control Details: <asp:Label runat="server" ID="lblControl" />
            </asp:Panel>
        </div>
        </form>
    </body>
    </html>
     
     
     
     
     
     

    No comments:

    Azure Storage Account Types

    Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...