locked
How to access(getvalue) of dymnically created Textboxes RRS feed

  • Question

  • User-1305878657 posted

    Hi Team,

    I am creating textboxes dynamically from the code behind C#.but problem is that in one save button i have to access these textboxes value for save into database.

       DataTable dt = new DataTable();

       dt = ObjUserFn.FnGetMIS_MasterSap_DATA("select (select elementname from QCElement where ElementID=qel.ElementID) TestResult,'' ElementValue,qel.MatrlID from QCElementLookup qel where qel.MatrlID='" + cboMatrlName.Value.ToString() + "'");

                if (dt.Rows.Count > 0)
                {
                    Panel2.Controls.Clear();
                    //Adding in tablular format
                    int rowCount = dt.Rows.Count;
                    int columnCount = Convert.ToInt32("1");
                    Table table = new Table();
                    table.ID = "table1";
                    for (int i = 0; i < rowCount; i++)
                    {
                        TableRow row = new TableRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            TableCell cell1 = new TableCell();
                            TableCell cell2 = new TableCell();
                            TextBox TxtBoxU = new TextBox();
                            Label lblU = new Label();
                           TxtBoxU.ID = "txt" + dt.Rows[i][0].ToString();
                            lblU.Text = dt.Rows[i][0].ToString();
                            cell1.Controls.Add(lblU);
                            cell2.Controls.Add(TxtBoxU);
                            row.Cells.Add(cell1);
                            row.Cells.Add(cell2);
                        }
                        table.Rows.Add(row);
                    }
                    Panel2.Controls.Add(table);
                }

    How to access these textboxes value in save button click.......

    Thursday, January 28, 2016 1:00 PM

Answers

  • User-2057865890 posted

    Hi mohan,

    i am finally adding table into panel not each control

    Take a look at the following sample.

    protected void Page_Load(object sender, EventArgs e)
    {           
        BindData();
    }
    
    protected void btnGet_Click(object sender, EventArgs e)
    {
        string value = "";
        foreach (TextBox textBox in Panel2.Controls.OfType<TextBox>())
        {
            value += textBox.Text + "\\n";
        }
    }
    
    private void BindData()
            {
                Panel2.Controls.Clear();
                DataTable dt = new DataTable();
    
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("TestResult", typeof(string)),
    
                                new DataColumn("ElementValue", typeof(string)),
    
                                new DataColumn("MatrlID",typeof(string)) });
    
                dt.Rows.Add("1", "TestA", "A");
                dt.Rows.Add("2", "TestB", "B");
                dt.Rows.Add("3", "TestB", "C");
                dt.Rows.Add("4", "TestB", "D");
    
                if (dt.Rows.Count > 0)
                {
                    Panel2.Controls.Clear();
                    //Adding in tablular format
                    int rowCount = dt.Rows.Count;
                    int columnCount = Convert.ToInt32("1");
                    Table table = new Table();
                    table.ID = "table1";
    
    
                    List<TextBox> t = new List<TextBox>();
                    for (int i = 0; i < rowCount; i++)
                    {
    
                        TableCell cell1 = new TableCell();
                        TableCell cell2 = new TableCell();
                        TextBox TxtBoxU = new TextBox();
                        Label lblU = new Label();
                        TxtBoxU.ID = "txt" + dt.Rows[i][0].ToString();
                        lblU.Text = dt.Rows[i][0].ToString();
                        cell1.Controls.Add(lblU);
                        cell2.Controls.Add(TxtBoxU);
    
    
                        Literal lt = new Literal();
                        lt.Text = "<br />";
    
                        Panel2.Controls.Add(lblU);
                        Panel2.Controls.Add(TxtBoxU);
                        Panel2.Controls.Add(lt);
                    }
    
                }
            }

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Panel ID="Panel2" runat="server"></asp:Panel>
            <asp:Button ID="btnGet" runat="server" Text="Button" OnClick="btnGet_Click" />
        </div>
        </form>
    </body>

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 2:52 AM

All replies

  • User-2057865890 posted

    Hi mohan,

    How to access these textboxes value in save button click.......

    When the Get Button is clicked, a loop is executed over all the TextBoxes present inside the Panel control.

    HTML Markup

    <asp:Panel ID="pnlTextBoxes" runat="server">
    
    </asp:Panel>
    
    <asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetTextBoxValues" />

    Getting values of dynamically created TextBoxes

    protected void GetTextBoxValues(object sender, EventArgs e)
    {
        string value = "";
        foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
        {
            value += textBox.Text + "\\n";
        }
    }

    Best Regards,

    Chris Zhao

    Friday, January 29, 2016 2:42 AM
  • User-1305878657 posted

    Thank u for reply i already tried this earlier but then also i not getting those textboxes values then i put this post.....


            string value = "";
            foreach (TextBox textBox in Panel2.Controls.OfType<TextBox>())
            {
                value += textBox.Text + "\\n";
            }

    Basically this part is not working in foreach loop Panel2.Controls.OfType<TextBox>()

    Friday, January 29, 2016 4:27 AM
  • User-2057865890 posted

    Hi mohan,

    Based on your code, you should add

    Panel2.Controls.Add(TxtBoxU);

    Best Regards,

    Chris Zhao

    Friday, January 29, 2016 8:09 AM
  • User-1305878657 posted

    i am finally adding table into panel not each control

    Friday, January 29, 2016 11:41 AM
  • User-2057865890 posted

    Hi mohan,

    i am finally adding table into panel not each control

    Take a look at the following sample.

    protected void Page_Load(object sender, EventArgs e)
    {           
        BindData();
    }
    
    protected void btnGet_Click(object sender, EventArgs e)
    {
        string value = "";
        foreach (TextBox textBox in Panel2.Controls.OfType<TextBox>())
        {
            value += textBox.Text + "\\n";
        }
    }
    
    private void BindData()
            {
                Panel2.Controls.Clear();
                DataTable dt = new DataTable();
    
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("TestResult", typeof(string)),
    
                                new DataColumn("ElementValue", typeof(string)),
    
                                new DataColumn("MatrlID",typeof(string)) });
    
                dt.Rows.Add("1", "TestA", "A");
                dt.Rows.Add("2", "TestB", "B");
                dt.Rows.Add("3", "TestB", "C");
                dt.Rows.Add("4", "TestB", "D");
    
                if (dt.Rows.Count > 0)
                {
                    Panel2.Controls.Clear();
                    //Adding in tablular format
                    int rowCount = dt.Rows.Count;
                    int columnCount = Convert.ToInt32("1");
                    Table table = new Table();
                    table.ID = "table1";
    
    
                    List<TextBox> t = new List<TextBox>();
                    for (int i = 0; i < rowCount; i++)
                    {
    
                        TableCell cell1 = new TableCell();
                        TableCell cell2 = new TableCell();
                        TextBox TxtBoxU = new TextBox();
                        Label lblU = new Label();
                        TxtBoxU.ID = "txt" + dt.Rows[i][0].ToString();
                        lblU.Text = dt.Rows[i][0].ToString();
                        cell1.Controls.Add(lblU);
                        cell2.Controls.Add(TxtBoxU);
    
    
                        Literal lt = new Literal();
                        lt.Text = "<br />";
    
                        Panel2.Controls.Add(lblU);
                        Panel2.Controls.Add(TxtBoxU);
                        Panel2.Controls.Add(lt);
                    }
    
                }
            }

    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Panel ID="Panel2" runat="server"></asp:Panel>
            <asp:Button ID="btnGet" runat="server" Text="Button" OnClick="btnGet_Click" />
        </div>
        </form>
    </body>

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 2:52 AM