How to create and collect textbox value at runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yogarajan
    New Member
    • Apr 2007
    • 115

    How to create and collect textbox value at runtime?

    Hi All

    I am create textbox and collect textbox value at runtime using c#

    my aspx code
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Add More" OnClick="Button1_Click" />
            <asp:Panel ID="Panel1" runat="server">
                <asp:Table ID="Table1" runat="server">
                    <asp:TableRow>
                        <asp:TableCell>Language</asp:TableCell>
                        <asp:TableCell>Read</asp:TableCell>
                        <asp:TableCell>Write</asp:TableCell>
                        <asp:TableCell>Speak</asp:TableCell>
                        
                    </asp:TableRow>
                    
                </asp:Table>
            </asp:Panel>
            
        </div>
        </form>
    </body>
    </html>
    my cs code

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class test : System.Web.UI.Page
    {
        
        //
        //System.Web.UI.WebControls.TextBox txt1 = new System.Web.UI.WebControls.TextBox();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //Panel1
            if (!IsPostBack)
            {
                ViewState["count"] = 0;
            }
            else
            {
               // gettextcheckboxval();
            }
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            gettextcheckboxval();
            
            ViewState["count"] = Convert.ToInt32(ViewState["count"])+1;
            //Response.Write(Convert.ToString(ViewState["count"]));
           
            for (int i = 0; i < Convert.ToInt32(ViewState["count"]); i++)
            {
    
    
                TableRow tr = new TableRow();
                TableCell td1 = new TableCell();
                TableCell td2 = new TableCell();
                TableCell td3 = new TableCell();
                TableCell td4 = new TableCell();
    
                TextBox textBoxes = new TextBox();
                textBoxes.ID = "TextBox" + i;
                textBoxes.Text = "English";
                
    
                CheckBox readchk = new CheckBox();
                readchk.ID = "readCheckBox" + i;
    
                CheckBox writechk = new CheckBox();
                writechk.ID = "writeCheckBox" + i;
    
                CheckBox speakchk = new CheckBox();
                speakchk.ID = "speakCheckBox" + i;
    
                td1.Controls.Add(textBoxes);
                td2.Controls.Add(readchk);
                td3.Controls.Add(writechk);
                td4.Controls.Add(speakchk);
    
                tr.Cells.Add(td1);
                tr.Cells.Add(td2);
                tr.Cells.Add(td3);
                tr.Cells.Add(td4);
    
                Table1.Rows.Add(tr);
    
    
                //Panel1.Controls.Add(txt);
            }
        }
    
        private void gettextcheckboxval()
        {
            for (int i = 0; i < Convert.ToInt32(ViewState["count"]); i++)
            {
                TextBox txtCtrl = (TextBox)Panel1.FindControl("TextBox" + (i + 1));
                if (txtCtrl != null)
                {
    
                    Response.Write((i+1)+" box value = "+txtCtrl.Text);
                    Response.Write("<br>");
                }
                else
                {
                    Response.Write("TextBox"+(i+1)+" not found");
                    Response.Write("<br>");
                }
            }
        }
    }
    but my output

    TextBox1 not found
    TextBox2 not found

    what is wrong with my code

    Pls guide me

    Thanks in Advance
Working...