Question Cannot read dynamically created textbox in gridview

iluilyas

New member
Joined
Apr 15, 2018
Messages
1
Programming Experience
10+
[FONT=&quot]Hi
I 'm programatically creating text boxes in gridview. But I'm unable to get the value from the text box.

[/FONT]
C#:
[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">
<table>
<tr>
<td style="vertical-align:top;">
<table>
<tr>
<th class="single_th" >Date</th>
</tr>
<tr>
<td>
<asp:UpdatePanel ID="upDate" runat="server">
<ContentTemplate>
<div>
<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" />
</asp:Calendar>
</div>
<div>
<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" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</td>
<td>   </td>
<td>
<asp:UpdatePanel ID="pnlIG" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<asp:GridView ID="gvPlan" runat="server"
ShowFooter="True" AutoGenerateColumns="true"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gvPlan_RowDeleting">
<Columns>
</Columns>
<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" />
</asp:GridView>
</div>
<div style="float:right;">
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="buttoncss" OnClick="btnSave_Click" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnGo" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</div>
</asp:Content>[/FONT]
[FONT=&quot][B]Code Page:[/B][/FONT]
[FONT=&quot]protected void btnGo_Click(object sender, EventArgs e)
{
Insert_Grid();
}[/FONT]
[FONT=&quot]private void Insert_Grid()
{
CreateGridView();
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";
row.Cells[0].Controls.Add(rbShift);[/FONT]
[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");
row.Cells[1].Controls.Add(txtID);[/FONT]
[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");
row.Cells[2].Controls.Add(txtName);[/FONT]
[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");
row.Cells[3].Controls.Add(txtDesig);[/FONT]
[FONT=&quot]// text box - EXTN
TextBox txtExtn = new TextBox();
txtExtn.ID = "tExtn" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtExtn.Attributes.Add("runat", "server");
row.Cells[4].Controls.Add(txtExtn);[/FONT]
[FONT=&quot]// text box - MOB
TextBox txtMob = new TextBox();
txtMob.ID = "tMob" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtMob.Attributes.Add("runat", "server");
row.Cells[5].Controls.Add(txtMob);[/FONT]
[FONT=&quot]// text box - REMARKS
TextBox txtRemrks = new TextBox();
txtRemrks.ID = "tRemrks" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtRemrks.Attributes.Add("runat", "server");
row.Cells[6].Controls.Add(txtRemrks);[/FONT]
[FONT=&quot]}[/FONT]
[FONT=&quot]
TextBox txn = (TextBox)gvPlan.Rows[0].Cells[1].FindControl("tID1");
txn.Focus();
}[/FONT]
[FONT=&quot]private void CreateGridView()
{[/FONT]
[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;
gvPlan.DataBind();[/FONT]
[FONT=&quot]}[/FONT]
[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;
rowIndex++;
}
}
}[/FONT]
[FONT=&quot]
"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]
 
Back
Top Bottom