Click here to Skip to main content
13,831,153 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi I 'm programatically creating text boxes in gridview. But I'm unable to get the value from the text box.

I have a text box where i get the number of rows to be created and on pressing "Go" button, it populates gridview with dynamically created text boxes.

I need to read the values from gridview text boxes and this is where i got stuck.

My Gridview:
<asp:GridView ID="gvPlan" runat="server" ShowFooter="True" AutoGenerateColumns="true">

Code Page:
protected void btnGo_Click(object sender, EventArgs e)

private void Insert_Grid()
   int numbers = int.Parse(txtRows.Text.Trim());
   int cellCount = gvPlan.Rows[0].Cells.Count;
   int rowsCount = gvPlan.Rows.Count;
   foreach (GridViewRow row in gvPlan.Rows)
      // text box - ID
    TextBox txtID = new TextBox();
    txtID.ID = "tID" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
    txtID.Attributes.Add("runat", "server");

      // text box - NAME
    TextBox txtName = new TextBox();
    txtName.ID = "tName" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
    txtName.Attributes.Add("runat", "server");
    txtName.Attributes.Add("ReadOnly", "true");

private void CreateGridView()
   int numbers = int.Parse(this.txtRows.Text.Trim());
   DataTable dt = new DataTable();
   dt.Columns.Add("ID", typeof(string));
   dt.Columns.Add("Name", typeof(string));
   for (int i = 0; i < numbers; i++)


      dt.Rows.Add("", "");


   ViewState["CurrentTable"] = dt;

   gvPlan.DataSource = dt;



protected void btnSave_Click(object sender, EventArgs e)


   int rowIndex = 0;

   if (ViewState["CurrentTable"] != null)


      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

      if (dtCurrentTable.Rows.Count > 0)
        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)


          //extract the TextBox values

          TextBox gtxtID = (TextBox)gvPlan.Rows[rowIndex].Cells[1].FindControl("tID" + (rowIndex+1));

          TextBox gtxtName = (TextBox)gvPlan.Rows[rowIndex].Cells[2].FindControl("tName" + (rowIndex + 1));

           gtxtName.Text = gtxtID.Text;





The issue is in "btnSave_Click" procedure where i dont seem to get the value from gtxtID. It cannot find the text box control and the object is null

Your help is much appreciated.


What I have tried:

Tried debugging code by code but in vain
Updated 16-Apr-18 3:22am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Maybe the answers here will be of help: How can i read a dynamically created textbox in gridview[^]

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

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190114.1 | Last Updated 16 Apr 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100