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
    Posts
    1
    Rep Power
    0

    Cannot read dynamically created textbox in gridview

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


    Code:
    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">
    <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>&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" />
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    </td>
    </tr>
    </table>
    </td>
    <td>&nbsp;&nbsp;&nbsp;</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>
    Code Page:
    protected void btnGo_Click(object sender, EventArgs e)
    {
    Insert_Grid();
    }
    private void Insert_Grid()
    {
    CreateGridView();
    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";
    row.Cells[0].Controls.Add(rbShift);
    // 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);
    // 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);
    // 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);
    // 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);
    // 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);
    // 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);
    }
    
    TextBox txn = (TextBox)gvPlan.Rows[0].Cells[1].FindControl("tID1");
    txn.Focus();
    }
    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;
    gvPlan.DataBind();
    }
    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++;
    }
    }
    }

    "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
    Posts
    1,958
    Rep Power
    151
    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

Bookmarks

Posting Permissions

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