 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 11th, 2008, 05:26 AM
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dynamically created control's values to database
I have created controls dynamically in ASP.NET 2.0 using C#.Following is the code :
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 3; i++)
{
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
Label lbl = new Label();
lbl.ID = "lbl1" + i;
lbl.Text = "Book Name : ";
tc1.Controls.Add(lbl);
tr1.Cells.Add(tc1);
TableCell tc2 = new TableCell();
TextBox txt = new TextBox();
txt.ID = "txtName" + i;
txt.Height = 15;
txt.TextMode = TextBoxMode.MultiLine;
txt.Attributes.Add("style","overflow:hidden"); //removes scroll bars symbols from a multiline textbox
tc2.Controls.Add(txt);
tr1.Cells.Add(tc2);
TableCell tc3 = new TableCell();
Label lbl4 = new Label();
lbl4.ID = "lbl5" + i;
lbl4.Text = "Subject : ";
tc3.Controls.Add(lbl4);
tr1.Cells.Add(tc3);
TableCell tc4 = new TableCell();
TextBox txt3 = new TextBox();
txt3.ID = "txtSubject" + i;
tc4.Controls.Add(txt3);
tr1.Cells.Add(tc4);
TableCell tc5 = new TableCell();
Label lbl5 = new Label();
lbl5.ID = "lbl6" + i;
lbl5.Text = "Author Name : ";
tc5.Controls.Add(lbl5);
tr1.Cells.Add(tc5);
TableCell tc6 = new TableCell();
TextBox txt4 = new TextBox();
txt4.ID = "txtAuthor" + i;
tc6.Controls.Add(txt4);
tr1.Cells.Add(tc6);
TableRow tr2 = new TableRow();
Table1.Rows.Add(tr1);
Table1.Rows.Add(tr2);
}
}
Accessed the values of these controls by the following code :
protected void Button1_Click(object sender, EventArgs e)
{
string name = "", subject = "", author = "";
//SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=sa;databa se=Books;Integrated Security=True");
//conn.Open();
//SqlCommand comm = new SqlCommand("INSERT INTO book values(@n,@s,@a)", conn);
foreach (TableRow tr in Table1.Rows)
{
foreach (TableCell tc in tr.Cells)
{
foreach (Control c in tc.Controls)
{
if (c is TextBox)
{
if (c.ID.StartsWith("txtName"))
{
TextBox txt = (TextBox)Table1.FindControl(c.ID);
name = txt.Text;
}
Response.Write(name);
if (c.ID.StartsWith("txtSubject"))
{
TextBox txt1 = (TextBox)Table1.FindControl(c.ID);
subject = txt1.Text;
}
Response.Write(subject);
if (c.ID.StartsWith("txtAuthor"))
{
TextBox txt2 = (TextBox)Table1.FindControl(c.ID);
author = txt2.Text;
}
Response.Write(author);
}
}
}
}
//comm.Parameters.Add("@n", SqlDbType.NVarChar).Value = name;
//comm.Parameters.Add("@s", SqlDbType.NVarChar).Value = subject;
//comm.Parameters.Add("@a", SqlDbType.NVarChar).Value = author;
//comm.ExecuteNonQuery();
}
I want to send the values of these controls to sql server database.The lines commented are those of the database coding.Now, the problem is that, although, I am getting the values of these controls but they are not in a regular form.Sometimes I am getting the same value twice and even thrice.When entering the values in the database a single record is inserted twice.What should I do ? Please help by giving some coding.I searched the net but its of no use.Pleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeease help.
|

August 11th, 2008, 05:33 AM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
OK, forgive me if I am being stupid here.. But what is dynamic about the control generation?
All the controls and their values are hard-coded. There is nothing dynamic about it, you are simply just making the program run slower by adding the controls at run time! ?!
Also, with your INSERT, you are trying to treat the data that is "dynamic" (i.e. non-static in structure) to a static structure by specifying parameters and the values for them?
So the main questions here are what do you view as dynamic? Why does it need to be dynamic? What data do you need to store and where do you need to store it?
Erm.. interesting.. code to read!
Rob
http://cantgrokwontgrok.blogspot.com
|

August 13th, 2008, 12:09 AM
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by robzyc
OK, forgive me if I am being stupid here.. But what is dynamic about the control generation?
All the controls and their values are hard-coded. There is nothing dynamic about it, you are simply just making the program run slower by adding the controls at run time! ?!
Also, with your INSERT, you are trying to treat the data that is "dynamic" (i.e. non-static in structure) to a static structure by specifying parameters and the values for them?
So the main questions here are what do you view as dynamic? Why does it need to be dynamic? What data do you need to store and where do you need to store it?
Erm.. interesting.. code to read! 
Rob
http://cantgrokwontgrok.blogspot.com
|
|

August 13th, 2008, 12:28 AM
|
Registered User
|
|
Join Date: Aug 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by robzyc
OK, forgive me if I am being stupid here.. But what is dynamic about the control generation?
All the controls and their values are hard-coded. There is nothing dynamic about it, you are simply just making the program run slower by adding the controls at run time! ?!
Also, with your INSERT, you are trying to treat the data that is "dynamic" (i.e. non-static in structure) to a static structure by specifying parameters and the values for them?
So the main questions here are what do you view as dynamic? Why does it need to be dynamic? What data do you need to store and where do you need to store it?
Erm.. interesting.. code to read! 
Rob
http://cantgrokwontgrok.blogspot.com
|
I want you to explain in detail why I want this.Actually, I am doing my course project in ASP.NET 2.0.The project is on Online Flight Reservation System.You might be knowing that, if there is someone else with you for a flight then when doing booking through internet, the textboxes and other controls generated for other person or persons depends upon the choice of the user.Like if there is only one person to go then that is a no problem state.But if there is someone else also then you select the no. of copassengers from the dropdownlist control and depending on that number those many textboxes etc., will be generated for each copassenger.Like in my case apart from the main user(which will be the credit card holder) there will be two textboxes and one dropdownlist control (for name,age and gender) for each copassenger with main user.I need to send the values of these controls to sql server database, based on which I will be generating the e-ticket.And the coding which I posted was not of the original project, it was of a sample project.Please help me as only one week is left.
|

August 13th, 2008, 12:49 AM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Then you need to seriously re-think your design.. You cant pass "many" records to the command, what you need to do is work out how many passengers have been entered onto the form, parse them, then pass each one to the stored procedure for saving.
As for the UI layer, you need to add the controls dynamically (i.e. you instantiate at RUN TIME based on what the user is doing (like click "add another passenger" button). There should be no "button1, button2 etc". If I want to book a reservation for 1, 2, 50 people, the code should have no idea.
Have a [serious] think.. This shouldnt be too challenging, especially if you break it down into smaller, more manageable chunks.
Rob
http://cantgrokwontgrok.blogspot.com
|
|
 |