Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have been trying to find a solution to it for the last 2 hours but I am not successful. I have a gridview and inside this view I have a templatefield. I am trying to access a textbox inside this grid. Here is the code in the designer for gridview:

C#
<asp:GridView ID="gvExcParts" runat="server" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" CellPadding="4" CellSpacing="2"
UseAccessibleHeader="False" Width="950px" onrowdatabound="gvSelectedItems_RowDataBound" Visible="False" OnRowEditing="gvExcParts_RowEditing" 
                    OnRowCancelingEdit="gvExcItems_RowCancelingEdit" OnRowUpdating="gvExcItems_RowUpdating" OnRowDeleting="gvExcItems_RowDeleting"
                    onrowcommand="gvExcItems_RowCommand" AllowSorting="true" AutoGenerateEditButton="true">
                    <RowStyle BackColor="#EEEEEE" BorderColor="White" BorderStyle="Solid"
                        BorderWidth="2px" />
                    <Columns>
   <asp:TemplateField HeaderText="Part Quantity" >
                        <ItemTemplate > <%#Eval("RMAPartQty")%>
                        </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtRMAPartQuantity" runat="server" width="60px" Text='<%#Eval("RMAPartQty")%>'></asp:TextBox>
                                <asp:FilteredTextBoxExtender ID="ftbeQtyEdit" runat="server" TargetControlID="txtRMAPartQuantity" ValidChars="1234567890" />
                            </EditItemTemplate>
                        </asp:TemplateField>
</Columns>
                    <HeaderStyle BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid"
                        BorderWidth="1px" Font-Bold="True" />
                    <EditRowStyle BackColor="#EEEEEE" />
                    <AlternatingRowStyle BackColor="#EFEFEF" />
                </asp:GridView>



I am trying to read the value of "txtRMAPartQuantity". My code for it is:

C#
for (int i = 0; i < gvExcParts.Rows.Count; i++)
{
  string t1 = gvExcParts.Rows[i].Cells[1].Text;
  TextBox txtQtyval1 = (TextBox)gvExcParts.Rows[i].Cells[1].FindControl("txtRMAPartQuantity");
  TextBox txtQtyval2 = gvExcParts.Rows[i].FindControl("txtRMAPartQuantity") as TextBox;
}


On my gvExcItems_RowUpdating event I am using the same FindControl() method and it successfully returns the value. But in my page load no matter what I try the value is always null. What I am doing wrong can someone help me?
Posted

Make sure you iterate through your gridview rows after you bind it to data.

C#
protected void Page_Load(object sender, EventArgs e)
       {

           if(!Page.IsPostBack)
           {

               gvExcParts.DataSource = yourDataSource;
               gvExcParts.DataBind();
               for (int i = 0; i < gvExcParts.Rows.Count; i++)
               {
                 TextBox text = (TextBox)gvExcParts.Rows[i].FindControl("txtRMAPartQuantity");
               }

          }
    }
 
Share this answer
 
Comments
wonder-FOOL 13-Jan-12 19:35pm    
with all my respect but your solution is useless. of course i am iterating after binding the gridview. there are other fields that i didnt show there just a bound field for example i can read its value but cant read the itemtemplate.. weird...
virang_21 13-Jan-12 19:46pm    
I just realized you put your textbox inside EditTemplate. Put it inside your ItemTemplate and you will be OK.
try this code ...
here h is row number

int h=0;
TextBox nt = (TextBox)Gridview1.Rows[h].Cells[1].FindControl("txt_Product_Name");
string myVal= nt.Text ;

by using FindControl method u can pick value of any control inside a grid view.

enjoy :)
 
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