Friday, June 18, 2010

How to find the control which caused a postback ( Part 2 : Inside the dedecated event handler) - Asp.net

This article demonstrate how to find the postback control inside the dedicated event handler. Simply we use command arguement propery of a button to after casting sender of the event to required type

<%@ Page Language="C#"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected void LinkClicked(object sender, EventArgs e)
        {
            LinkButton linkButton = sender as LinkButton;
            if (linkButton != null)
                this.Response.Write(linkButton.CommandArgument);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:LinkButton runat="server"  ID="LinkButton1" 
            CommandArgument="LinkButton1" OnClick="LinkClicked" Text="LinkButton 1" />
        <asp:LinkButton runat="server"  ID="LinkButton2" 
            CommandArgument="LinkButton2" OnClick="LinkClicked" Text="LinkButton 2" />
        <asp:LinkButton runat="server"  ID="LinkButton3" 
            CommandArgument="LinkButton3" OnClick="LinkClicked" Text="LinkButton 3" />
        <asp:LinkButton runat="server"  ID="LinkButton4" 
            CommandArgument="LinkButton4" OnClick="LinkClicked" Text="LinkButton 4" />
        <asp:LinkButton runat="server"  ID="LinkButton5" 
            CommandArgument="LinkButton5" OnClick="LinkClicked" Text="LinkButton 5" />
    </form>
</body>
</html>

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...