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

How can save the value of dynamically created textbox

P: 22
Good evening..
how can i save the value of dynamically created textbox in data base
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. using System.Data.SqlClient;
  12. public partial class frndslambook : System.Web.UI.Page
  13. {
  14.     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  15.      SqlCommand com1;
  16.     SqlCommand com;
  17.     SqlDataReader dr1;
  18.     static string[] exp;
  19.      static string exp1;
  20.     static long c;
  21.     TextBox[] textBoxArr = new TextBox[c + 1];  
  22.     protected void Page_Load(object sender, EventArgs e)
  23.     {
  24.        com1 = new SqlCommand("Select * From slambook Where username=@username", con);
  25.      com1.Parameters.Add("@username", SqlDbType.NVarChar).Value = Session["userfrndname"].ToString();
  26.         try
  27.         {
  28.             if (con.State == ConnectionState.Closed)
  29.                 con.Open();
  30.             dr1 = com1.ExecuteReader();
  31.             if (dr1.Read())
  32.             {
  33.  
  34.  
  35.                 string exp1 = dr1["slambookfields"].ToString();
  36.                long c = Convert.ToInt64(exp1.Length);
  37.                 Label[] l1 = new Label[c + 1];//array of lables
  38.               TextBox[] textBoxArr = new TextBox[c + 1];//array of textboxes
  39.  
  40.                 exp = (exp1).Trim('^').Split('^');
  41.                 for (int i = 0; i < exp.Length; i++)
  42.                 {
  43.                     Panel frndstextBoxLabelGroup = new Panel();
  44.                     l1[i] = new Label();
  45.                     l1[i].ID = "frndslambooklabel" + i.ToString();
  46.                     l1[i].Text = exp[i].ToString();
  47.                     l1[i].Visible = true;
  48.  
  49.  
  50.                     textBoxArr[i] = new TextBox();
  51.                     textBoxArr[i].ID = "frndslambooktextbox" + i.ToString();
  52.                     textBoxArr[i].Visible = true;
  53.                     //Initializing the TextBox so that it is not rendered in the browser
  54.                     frndstextBoxLabelGroup.ID = "frndstextBoxLabelGroup" + i.ToString();
  55.                     fspfrndslambook.Visible = true;
  56.                     // Pnl_TextBox.Controls.Add(br);
  57.                     frndstextBoxLabelGroup.Controls.Add(l1[i]);
  58.  
  59.                     frndstextBoxLabelGroup.Controls.Add(textBoxArr[i]);
  60.                     fspfrndslambook.Controls.Add(frndstextBoxLabelGroup);
  61.  
  62.  
  63.                 }
  64.             }
  65.  
  66.  
  67.         }
  68.         catch (Exception exc)
  69.         {
  70.         }
  71.         finally
  72.         {
  73.             con.Close();
  74.         }
  75.  
  76.     }
  77.     protected void fsbfill_Click(object sender, EventArgs e)
  78.     {
  79.         com = new SqlCommand("Insert Into slambookans (slambookfields_ans,sender,receiver) Values(@slambookfields_ans,@sender,@receiver)", con);
  80.        for (int i = 0; i < exp.Length; i++)
  81.         //{
  82.             //textBoxArr[i] = new TextBox();
  83.          com.Parameters.Add("@slambookfields_ans", SqlDbType.NVarChar).Value =textBoxArr[i].Text;
  84.  
  85.         }
  86.         com.Parameters.Add("@sender", SqlDbType.NVarChar).Value = Session["username"].ToString();
  87.         com.Parameters.Add("@receiver", SqlDbType.NVarChar).Value = Session["userfrndname"].ToString();
  88.              try
  89.             {
  90.                 if (con.State == ConnectionState.Closed)
  91.                     con.Open();
  92.                 int i=com.ExecuteNonQuery();
  93.                if(i!=0)
  94.                Response.Write("Success");
  95.                else
  96.                 Response.Write("Fail");
  97.              }
  98.         catch(Exception exc)
  99.         {
  100.         }
  101.         finally
  102.         {
  103.             con.Close();
  104.         }
  105.     }
  106. }
  107.  
  108.  
in the above code the textboxArr is dynamically created..and i want to save the value of that button at fsbfill_click.how can i do this?i want to save the value in one tuple of database.
Oct 12 '10 #1
Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
I'm not exactly sure where to start...

Actually, maybe you should start by reading the How to use dynamic controls in ASP.NET article.

You are experiencing problems in your application because you are not considering how the ASP.NET-life-cycle effects how controls and events are treated.

-Frinny
Oct 15 '10 #2

danp129
Expert 100+
P: 321
Only creating the controls when page.isPostBack is false would be a good start.
Oct 15 '10 #3

Frinavale
Expert Mod 5K+
P: 9,731
Actually you Must create the controls every time the page is loaded or else you will not be able to use them.

You should be doing this in the Page Init event though...so that events and properties (etc) can be loaded for the controls when the ViewState is loaded. Doing this in your Page Load event is not going to help you because that event occurs after the ViewState is loaded (all of the controls are created).



-Frinny
Oct 15 '10 #4

danp129
Expert 100+
P: 321
Oh yeah, it's been a while since I've last done that :P
Oct 15 '10 #5

Post your reply

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