473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How can save the value of dynamically created textbox

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
4 1850
Frinavale
9,735 Expert Mod 8TB
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
323 Expert 256MB
Only creating the controls when page.isPostBack is false would be a good start.
Oct 15 '10 #3
Frinavale
9,735 Expert Mod 8TB
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
323 Expert 256MB
Oh yeah, it's been a while since I've last done that :P
Oct 15 '10 #5

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

Similar topics

3
by: crjunk | last post by:
I have a web page that displays multiple records for a company. The input text boxes that display my data are created dynamically. I'm creating these input boxes dynamically because the number of...
2
by: utterberg | last post by:
Can anyone help me with this problem? I dynamically creates several textboxes using a placeholder. This works fine. But is there a way for me to loop through theese textboxes and retrive its value...
5
by: Jack Johnston | last post by:
Hi there, I am dynamically created a table at runtime on an aspx form. The last cell of each row contains a TextBox in which the user will enter numeric values into. Each textbox I create is...
2
by: mwhalen | last post by:
Hi All, I've dynmaically created a textbox, but I can't edit it. When I click on it, the cursor flashes for a second, but then goes away and I can't enter any text or do anything with the value...
2
by: JaM | last post by:
Hi all, I have created a gridview vith dynamic textbox columns (they are in variable number, it depends on what things I select from database) aspx code:...
0
by: Silver Oak | last post by:
I have a DataGrid in which one of the columns is TemplateColumn that was created dynamically using iTemplate. I would like to have multi-row editing capability on the DataGrid. I'm trying to...
2
by: HHAAPPYY | last post by:
Hi I am trying to pass value of the textbox along with another value to the query string. when i am retriving the txtbox value it always shows me null, my senario is like this On my...
3
by: raghulvarma | last post by:
I have created only one object for the textbox and that particular textbox is being repeated as many times I want.But if I want to add the values in the database from each and every textbox which...
1
coolv
by: coolv | last post by:
Hello everyone, I m generating dynamic textbox depending on the value selected from the dropdownlist(DropDownList1_SelectedIndexChanged).eg: if i select 3 from dropdownlist,then 3 textbox...
1
by: manila | last post by:
Can u help me I have already created the dynamic textbox.but i don't know the code to insert the records and save it in database through this textbox.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.