Question Cannot read dynamically created textbox in gridview


New member
Apr 15, 2018
Programming Experience
I 'm programatically creating text boxes in gridview. But I'm unable to get the value from the text box.

[FONT=&quot][B]ASPX page:[/B][/FONT]
[FONT=&quot]<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="newplan.aspx.cs" Inherits="newplan" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="row">
<div class="col-md-4">
<td style="vertical-align:top;">
<th class="single_th" >Date</th>
<asp:UpdatePanel ID="upDate" runat="server">
<asp:Calendar ID="calDate" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Arial" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<strong>Insert No of Rows:</strong>  <asp:TextBox ID="txtRows" runat="server" Width="30px" />  
<asp:Button ID="btnGo" runat="server" CssClass="buttoncss" Text="Go" OnClick="btnGo_Click" />
<td>   </td>
<asp:UpdatePanel ID="pnlIG" runat="server" UpdateMode="Conditional">
<asp:GridView ID="gvPlan" runat="server"
ShowFooter="True" AutoGenerateColumns="true"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gvPlan_RowDeleting">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<div style="float:right;">
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="buttoncss" OnClick="btnSave_Click" />
<asp:AsyncPostBackTrigger ControlID="btnGo" EventName="Click" />
[FONT=&quot][B]Code Page:[/B][/FONT]
[FONT=&quot]protected void btnGo_Click(object sender, EventArgs e)
[FONT=&quot]private void Insert_Grid()
int numbers = int.Parse(txtRows.Text.Trim());[/FONT]
[FONT=&quot]int cellCount = gvPlan.Rows[0].Cells.Count;
int rowsCount = gvPlan.Rows.Count;[/FONT]
[FONT=&quot]foreach (GridViewRow row in gvPlan.Rows)
// radio button - SHIFT
RadioButtonList rbShift = new RadioButtonList();
rbShift.ID = "rShift" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
rbShift.Items.Add(new ListItem("D", "D"));
rbShift.Items.Add(new ListItem("N", "N"));
rbShift.RepeatDirection = RepeatDirection.Horizontal;
rbShift.Attributes.Add("runat", "server");
rbShift.SelectedValue = "D";
[FONT=&quot]// text box - ID
TextBox txtID = new TextBox();
txtID.ID = "tID" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtID.Attributes.Add("runat", "server");
//txtID.Attributes.Add("EnableViewState", "false");
//txtID.Attributes.Add("AutoPostBack", "true");
//txtID.Attributes.Add("OnTextChanged", "txtID_TextChanged");
[FONT=&quot]// 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");
[FONT=&quot]// text box - DESIGNATION
TextBox txtDesig = new TextBox();
txtDesig.ID = "tDesig" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtDesig.Attributes.Add("runat", "server");
txtDesig.Attributes.Add("ReadOnly", "true");
[FONT=&quot]// text box - EXTN
TextBox txtExtn = new TextBox();
txtExtn.ID = "tExtn" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtExtn.Attributes.Add("runat", "server");
[FONT=&quot]// text box - MOB
TextBox txtMob = new TextBox();
txtMob.ID = "tMob" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtMob.Attributes.Add("runat", "server");
[FONT=&quot]// text box - REMARKS
TextBox txtRemrks = new TextBox();
txtRemrks.ID = "tRemrks" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtRemrks.Attributes.Add("runat", "server");
TextBox txn = (TextBox)gvPlan.Rows[0].Cells[1].FindControl("tID1");
[FONT=&quot]private void CreateGridView()
[FONT=&quot]int numbers = int.Parse(this.txtRows.Text.Trim());
DataTable dt = new DataTable();
//you can add as many columns you want
dt.Columns.Add("Shift", typeof(string));
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Desig", typeof(string));
dt.Columns.Add("Extn", typeof(string));
dt.Columns.Add("Mob", typeof(string));
dt.Columns.Add("Remrks", typeof(string));
for (int i = 0; i < numbers; i++)
//dont forget to add null values in each column
dt.Rows.Add("", "", "", "", "", "", "");
ViewState["CurrentTable"] = dt;
gvPlan.DataSource = dt;
[FONT=&quot]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;
"btnSave_Click" procedure is where i have this issue.
Where am I going wrong. Your help is much appreciated.
Regards, Ilyas[/FONT]
I'm not sure whether I could help anyway because I don't use Web Forms but you've made it very hard to help by posting hard to read code. Make sure to copy and paste directly so as to retain indenting, which is critical to readability. Also, post ASPX and C# code separately. Put only code within the code formatting tags and post the C# code like this:

[xcode=c#]your code here[/xcode]
Top Bottom