1
votes

I have a page which contains gridview and button now i want to add gridview values to database table when i click on button, but when i click on button and try to get value using find control its not showing any value it shows value as blank. my code is given below

aspx page
<%@ Page Title="" Language="C#" MasterPageFile="Masters.Master" AutoEventWireup="true" EnableEventValidation ="false" CodeBehind="test.aspx.cs" Inherits="test" UICulture="hi-IN" Culture="hi-IN" %>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="GridView1_DataBound"
                        OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="White" 
                        BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">

<asp:TemplateField Visible="true">
                                <HeaderTemplate>
                                    <asp:Label ID="lbl4" runat="server" Text="test."></asp:Label>
                                </HeaderTemplate>
                                <ItemTemplate>

                                    <asp:Label ID="lbltestno" runat="server" Text='<%# Bind("testing") %>' Visible="true"></asp:Label><br style="mso-data-placement:same-cell;"/>


                                </ItemTemplate>
                            </asp:TemplateField>

.cs page
on button click
 foreach (GridViewRow GvRow in GridView1.Rows)
            {

                Label lblvinnos = (Label)GvRow.FindControl("lbltestno");
}

Please note that this page is working fine when i use without master page but when i am using master page its showing find control values as null

I just checked its finding textbox value not of labels any particular reason?

3
what is your textbox code where using in .Aspx. - stefan
can someone help me i have 2 header in grid view and when i am using for each loop it take one header as row due to which my value fail can you suggest what to do - hamesh anand

3 Answers

1
votes

Try in Button cliked event like:

int indexcount = 0;

foreach (GridViewRow row in GridView1.Rows)
{
    // get value from label on each row in gridview
    string value = (GridView1.Rows[indexcount].FindControl("lbltestno") as Label).Text;

    indexcount++;
}
0
votes

You have to find it in cell, cell_index is actual index (starts with 0) of cell where control resides.

Label lblvinnos = (Label)GvRow.Cells[cell_index].FindControl("lbltestno");
0
votes

create button inside a template field

trigger it inside your gridview_rowcommand method

applies per row only

GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

string your_variable = Convert.ToDecimal(((TextBox)row.FindControl("your control here")).Text);

hope it helps