By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

How to create and collect textbox value at runtime?

100+
P: 115
Hi All

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

my aspx code
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:Button ID="Button1" runat="server" Text="Add More" OnClick="Button1_Click" />
  13.         <asp:Panel ID="Panel1" runat="server">
  14.             <asp:Table ID="Table1" runat="server">
  15.                 <asp:TableRow>
  16.                     <asp:TableCell>Language</asp:TableCell>
  17.                     <asp:TableCell>Read</asp:TableCell>
  18.                     <asp:TableCell>Write</asp:TableCell>
  19.                     <asp:TableCell>Speak</asp:TableCell>
  20.  
  21.                 </asp:TableRow>
  22.  
  23.             </asp:Table>
  24.         </asp:Panel>
  25.  
  26.     </div>
  27.     </form>
  28. </body>
  29. </html>
my cs code

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class test : System.Web.UI.Page
  13. {
  14.  
  15.     //
  16.     //System.Web.UI.WebControls.TextBox txt1 = new System.Web.UI.WebControls.TextBox();
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.         //Panel1
  21.         if (!IsPostBack)
  22.         {
  23.             ViewState["count"] = 0;
  24.         }
  25.         else
  26.         {
  27.            // gettextcheckboxval();
  28.         }
  29.  
  30.     }
  31.     protected void Button1_Click(object sender, EventArgs e)
  32.     {
  33.         gettextcheckboxval();
  34.  
  35.         ViewState["count"] = Convert.ToInt32(ViewState["count"])+1;
  36.         //Response.Write(Convert.ToString(ViewState["count"]));
  37.  
  38.         for (int i = 0; i < Convert.ToInt32(ViewState["count"]); i++)
  39.         {
  40.  
  41.  
  42.             TableRow tr = new TableRow();
  43.             TableCell td1 = new TableCell();
  44.             TableCell td2 = new TableCell();
  45.             TableCell td3 = new TableCell();
  46.             TableCell td4 = new TableCell();
  47.  
  48.             TextBox textBoxes = new TextBox();
  49.             textBoxes.ID = "TextBox" + i;
  50.             textBoxes.Text = "English";
  51.  
  52.  
  53.             CheckBox readchk = new CheckBox();
  54.             readchk.ID = "readCheckBox" + i;
  55.  
  56.             CheckBox writechk = new CheckBox();
  57.             writechk.ID = "writeCheckBox" + i;
  58.  
  59.             CheckBox speakchk = new CheckBox();
  60.             speakchk.ID = "speakCheckBox" + i;
  61.  
  62.             td1.Controls.Add(textBoxes);
  63.             td2.Controls.Add(readchk);
  64.             td3.Controls.Add(writechk);
  65.             td4.Controls.Add(speakchk);
  66.  
  67.             tr.Cells.Add(td1);
  68.             tr.Cells.Add(td2);
  69.             tr.Cells.Add(td3);
  70.             tr.Cells.Add(td4);
  71.  
  72.             Table1.Rows.Add(tr);
  73.  
  74.  
  75.             //Panel1.Controls.Add(txt);
  76.         }
  77.     }
  78.  
  79.     private void gettextcheckboxval()
  80.     {
  81.         for (int i = 0; i < Convert.ToInt32(ViewState["count"]); i++)
  82.         {
  83.             TextBox txtCtrl = (TextBox)Panel1.FindControl("TextBox" + (i + 1));
  84.             if (txtCtrl != null)
  85.             {
  86.  
  87.                 Response.Write((i+1)+" box value = "+txtCtrl.Text);
  88.                 Response.Write("<br>");
  89.             }
  90.             else
  91.             {
  92.                 Response.Write("TextBox"+(i+1)+" not found");
  93.                 Response.Write("<br>");
  94.             }
  95.         }
  96.     }
  97. }
  98.  
but my output

TextBox1 not found
TextBox2 not found

what is wrong with my code

Pls guide me

Thanks in Advance
Mar 2 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.