ASP.net GridView Extra Buttons - click event

Hi

I have an ASP.net GridView with 5 extra button columns.
How do I run code when Button2 is clicked that returns the row index.
<asp:GridView ID="GridView2" runat="server" BackColor="White" 
                        BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                        Height="116px" Width="795px">
                                    <Columns> 
                <asp:TemplateField HeaderText="5. Strong">
                   <ItemTemplate>
                       <asp:Button ID="Button1" runat="server" />
                  </ItemTemplate>
               </asp:TemplateField> 
                      <asp:TemplateField HeaderText="4. Moderate">
                   <ItemTemplate>
                       <asp:Button ID="Button2" runat="server" />
                  </ItemTemplate>
               </asp:TemplateField> 
                      <asp:TemplateField HeaderText="3. Adequate">
                   <ItemTemplate>
                       <asp:Button ID="Button3" runat="server" />
                  </ItemTemplate>
               </asp:TemplateField> 
                      <asp:TemplateField HeaderText="2. Weaker">
                   <ItemTemplate>
                       <asp:Button ID="Button4" runat="server" />
                  </ItemTemplate>
               </asp:TemplateField> 
                         <asp:TemplateField HeaderText="1. Weakest">
                   <ItemTemplate>
                       <asp:Button ID="Button5" runat="server" />
                  </ItemTemplate>
               </asp:TemplateField> 
            </Columns> 
                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <SortedAscendingCellStyle BackColor="#EDF6F6" />
                        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                        <SortedDescendingCellStyle BackColor="#D6DFDF" />
                        <SortedDescendingHeaderStyle BackColor="#002876" />
                    </asp:GridView>

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Ivo StoykovConnect With a Mentor Commented:
try something like the snippet below

HTH

Ivo Stoykov
protected void Button2Click(object sender, System.EventArgs e)
    {
        //Get the button that raised the event
        Button btn = (Button)sender;

        //Get the row that contains this button
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;

        //Get rowindex
        int rowindex = gvr.RowIndex;
    }

Open in new window

0
 
kovilpattiBaluConnect With a Mentor Commented:
Hi,

Add commandArgument/CommandName for buttons
ie:
 <asp:TemplateField HeaderText="5. Strong">
                   <ItemTemplate>
                       <asp:Button ID="Button1" runat="server" CommandName="Button1Click"/>
                  </ItemTemplate>
               </asp:TemplateField>

in server side, "GridView2_ItemCommand" event, check the command argument and write the codes..
ie:
 if (e.Item != null)
            {
                switch(e.CommandName)
                {
                    case "Button1Click":
                        {
                      // your codes      
                            break;
                        }
                    case "Button2Click":
                        {
                  // your codes                            
                            break;
                        }
                    case "Button3Click":
                  // your codes                            
                            break;
            }
}
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thank you for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.