Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to write the Button event in gridview (dynamic button)
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    Width="180px" AutoGenerateEditButton="True"
                    onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                <asp:TemplateField>
                <ItemTemplate>
                <asp:Label ID="l1" runat="server" Text='<%#Eval("sno")%>' >                </asp:Label>
                    <br>
                </br>
                <asp:Button ID="b1" runat="server" Text="sms"  CommandName="sms" />//i want to write the event for this button
                <asp:Button ID="B21" runat="server" Text="mail"/>
                </ItemTemplate>
                </asp:TemplateField>
                </Columns>
                </asp:GridView>
Posted

you need to use OnRowCommand event for that.

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    Width="180px" AutoGenerateEditButton="True"
OnRowCommand=GridView1_RowCommand
onselectedindexchanged="GridView1_SelectedIndexChanged">



C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "sms":
                    {
                        //your code here...
                        break;
                    }
                default:
                   break;

        }
}


If this helped you then please Vote and mark it as answer
 
Share this answer
 
Comments
sampath55 12-Oct-10 2:53am    
WHEN I CLICK THE THAT SMS BUTTON THIS IS ERROR IS COMING
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
NMehta83 12-Oct-10 4:11am    
make EnableEventValidation="false". and then let me know.
XML
<%@ Page Language="C#" AutoEventWireup="true"   EnableEventValidation="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Button ID="b1" runat="server" Text="sms"  CommandName="sms" onclick="B1_Click" />


C#
protected void B1_Click(object sender, EventArgs e)
    {
        Button1.Text = "santhu";
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900