Click here to Skip to main content
15,870,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview
without using SelectedIndexChanged, how can i retrieve the value of each row from gridview when click on each button in each row?

this is my aspx code
VB
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="ProductID" DataSourceID="SqlDataSource1" 
                ShowHeader="False" AllowPaging="True" BorderColor="White" CellPadding="6" 
                GridLines="None" Height="100px" Width="800px">
<Columns>
                   
<asp:TemplateField>
 <ItemTemplate>
 Card Name:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
 <br />
Cost :
  <asp:Label ID="Label2" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductImgID") %>' ></asp:Label>
 <asp:Image ID="Image3" runat="server" ImageUrl='<%# Eval("ProductImgUrl", "images/{0}") %>'  />
                          
                            <br />
<asp:Button ID="btnAddProduct" runat="server" Text="Add" />
                            <br />
                         
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
Posted
Updated 10-Jan-12 13:24pm
v3

1 solution

Hi,

write a button click event for the btnAddProduct. In the button click write the below code,

    //get the reference for the button
    Button btn = (Button)sender;
    //Get reference of the gridview row of the button clicked.
    GridViewRow gvReference = (GridViewRow)(btn.Parent.Parent);

    // Get the text of the label
String strLabel1  = ((Label)gvReference.FindControl("Label1")).Text.ToUpper();
 ...
 ...


or

Call a javascript function on the btnAddProduct click to get the values.
Hope this helps.
 
Share this answer
 
Comments
cutexxbaby 11-Jan-12 3:32am    
what about if it is a text box in the gridview, how am i able to retrieve
manognya kota 11-Jan-12 3:35am    
I guess that should be same as the above.
String strtext= ((TextBox)gvReference.FindControl("urTxtboxid")).Text();
I have never used this.This should work.Please check

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