Results 1 to 2 of 2

Thread: Cannot read dynamically created textbox in gridview

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Rep Power

    Cannot read dynamically created textbox in gridview

    I 'm programatically creating text boxes in gridview. But I'm unable to get the value from the text box.

    ASPX page:
    <%@ 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>&nbsp;&nbsp;<asp:TextBox ID="txtRows" runat="server" Width="30px" />&nbsp;&nbsp;
    <asp:Button ID="btnGo" runat="server" CssClass="buttoncss" Text="Go" OnClick="btnGo_Click" />
    <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" />
    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)
    // 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";
    // 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");
    // 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");
    // 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");
    // text box - EXTN
    TextBox txtExtn = new TextBox();
    txtExtn.ID = "tExtn" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
    txtExtn.Attributes.Add("runat", "server");
    // text box - MOB
    TextBox txtMob = new TextBox();
    txtMob.ID = "tMob" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
    txtMob.Attributes.Add("runat", "server");
    // 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");
    private void CreateGridView()
    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;
    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

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Rep Power
    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]

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2018, 8:04 AM
  2. Question Subtract a gridview textbox value from another textbox
    By lholds in forum Web Forms
    Replies: 0
    Last Post: 04-08-2013, 5:09 PM
  3. Question adding item to already created dataset and datatable
    By jithin in forum ADO.NET
    Replies: 1
    Last Post: 12-02-2012, 3:48 AM
  4. Question Menu created using sitemap is not working as needed
    By hiprashanth in forum Menus & Toolbars
    Replies: 0
    Last Post: 05-15-2012, 8:16 AM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts