Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

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

0
Murray Brown
Asked:
Murray Brown
2 Solutions
 
Ivo StoykovCommented:
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
 
kovilpattiBaluCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now