Click here to Skip to main content
14,773,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have Created multiple text boxes inside gridview at run time.
I have done this successfully.Now I want add a value to text box and save it.
but I am getting null when I call the control.

Following is my code

if (e.Row.RowType == DataControlRowType.DataRow)
         for (int i = 0; i >4; i++)
             TextBox txt = new TextBox();
             txt.CssClass = "SmallTextBoxDate";
             txt.Attributes.Add("runat", "server");
             txt.EnableViewState = true;
             txt.ID = "TextBox"+ (i).ToString();
             e.Row.Cells[i].Text = "";


now at save button when i try to access value I get null value.

TextBox txtname = ((TextBox)gvEmployee.FindControl("TextBox[1]"));// I get null here
string a = txtname.Text;

Reading many posts I have also recreate the control at postback, but after creating control i add value and then again button click event we get no controls.Pls help I have spend weeks on this.
Naveen.Sanagasetti 7-Oct-15 9:28am
Could you please try like below.
Use Proper Id tag

txt.ID = "txtVal"+ i.ToString();

And find that control like below
TextBox txtname = ((TextBox)gvEmployee.FindControl("txtVal1"));
Richard Deeming 7-Oct-15 10:41am
The control you've added is not a child of the gridview; it's a child of each row within the gridview.

You need to call FindControl on the specific row you're trying to read the value from.
ManeeshaPP 8-Oct-15 0:29am
thank you fro your reply..
1. Naveen I have already tryid that... not working.
2.Richard Deeming--Ya I have done this for every row... just could not post all code here..
I am getting value of the text box which is assigned during pageload..
but I am not able to get value when I change that value and click a button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900