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:

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...