473,320 Members | 2,147 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,320 software developers and data experts.

How to: Dynamic Control Creation: ASP.NET / C#

I need to create textboxes in real-time, the actual number of which is
determine by a result from a database query. I have been able to create the
controls, and then add them to the ASPX page. However, when i submit the
page, i am unable to read the values entered in these new textboxes.

I have first tried to use System.Web.UI.WebControls.TextBox namespace,
however, i was unable to read any value from my textboxes using the following
code:
string sQuestion = txtQuestion.Text.ToString().Trim();

I then tried using the System.Web.UI.HtmlControls.HtmlInputText namespace,
and again, was able to create and add the controls to my ASPX page. However,
i am only able to read the first value from the array of textbox controls
created at runtime w/ the following code:
string sAnswer = txtAnswers.Value.ToString();

My dynamic control creation code is:

for (int i = 0; i < iCtr; i++)
txtAnswers = new HtmlInputText();
if (sNewQuestion == "0")
{
txtAnswers.Value =
ds.Tables["Answers"].Rows[i]["vStudyAText"].ToString).Trim();
}
txtAnswers.ID = "txtAnswers";
this.Controls.AddAt(this.Controls.IndexOf(PlaceHol der1), txtAnswers);
}

Any suggestions how to dynamically create these textboxes and then easily
read their values? Using the current code, the values should be returned in
string separated by comas. However, i am open to whatever solution.

Thanks,
Nov 16 '05 #1
2 16219
Here's a tool that someone pointed out a while back that helps with
managing the creation and use of dynamic controls. Hope it helps.

http://www.denisbauer.com/ASPNETCont...aceholder.aspx

Have A Better One!

John M Deal, MCP
Necessity Software

charliewest wrote:
I need to create textboxes in real-time, the actual number of which is
determine by a result from a database query. I have been able to create the
controls, and then add them to the ASPX page. However, when i submit the
page, i am unable to read the values entered in these new textboxes.

I have first tried to use System.Web.UI.WebControls.TextBox namespace,
however, i was unable to read any value from my textboxes using the following
code:
string sQuestion = txtQuestion.Text.ToString().Trim();

I then tried using the System.Web.UI.HtmlControls.HtmlInputText namespace,
and again, was able to create and add the controls to my ASPX page. However,
i am only able to read the first value from the array of textbox controls
created at runtime w/ the following code:
string sAnswer = txtAnswers.Value.ToString();

My dynamic control creation code is:

for (int i = 0; i < iCtr; i++)
txtAnswers = new HtmlInputText();
if (sNewQuestion == "0")
{
txtAnswers.Value =
ds.Tables["Answers"].Rows[i]["vStudyAText"].ToString).Trim();
}
txtAnswers.ID = "txtAnswers";
this.Controls.AddAt(this.Controls.IndexOf(PlaceHol der1), txtAnswers);
}

Any suggestions how to dynamically create these textboxes and then easily
read their values? Using the current code, the values should be returned in
string separated by comas. However, i am open to whatever solution.

Thanks,

Nov 16 '05 #2
try and set the enableviewstate property to true for the dynamic text control.

With Regards
Shane Sukul
MCSD MCAD

"charliewest" wrote:
I need to create textboxes in real-time, the actual number of which is
determine by a result from a database query. I have been able to create the
controls, and then add them to the ASPX page. However, when i submit the
page, i am unable to read the values entered in these new textboxes.

I have first tried to use System.Web.UI.WebControls.TextBox namespace,
however, i was unable to read any value from my textboxes using the following
code:
string sQuestion = txtQuestion.Text.ToString().Trim();

I then tried using the System.Web.UI.HtmlControls.HtmlInputText namespace,
and again, was able to create and add the controls to my ASPX page. However,
i am only able to read the first value from the array of textbox controls
created at runtime w/ the following code:
string sAnswer = txtAnswers.Value.ToString();

My dynamic control creation code is:

for (int i = 0; i < iCtr; i++)
txtAnswers = new HtmlInputText();
if (sNewQuestion == "0")
{
txtAnswers.Value =
ds.Tables["Answers"].Rows[i]["vStudyAText"].ToString).Trim();
}
txtAnswers.ID = "txtAnswers";
this.Controls.AddAt(this.Controls.IndexOf(PlaceHol der1), txtAnswers);
}

Any suggestions how to dynamically create these textboxes and then easily
read their values? Using the current code, the values should be returned in
string separated by comas. However, i am open to whatever solution.

Thanks,

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
2
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired...
4
by: spx27 | last post by:
Does anyone know the best way to handle passing a dynamic menu generated from a login from one asp.net page to another. Would it be better to recreate the menu on each page? Save the menu as a...
1
by: Homam | last post by:
So I have a composite paging control that shoulld be positioned on the page like this: PagNav ResultSetDisplay PagNav I know that I can't resuse the PagNav more than once in the form, so I...
3
by: vodafone | last post by:
Hy all I've a little problem. I need to write a dynamic page that render control according to validation status return from previous control validation status. To be clear, I've page that...
0
by: Prodip K Saha | last post by:
Many Programmers stay away from loading User Control dynamically on the Web Form because of the complexity associated with it's state management and event handling. It is easy to load a User...
0
by: Paul | last post by:
Hi! I have been wondering about a design issue for some time now and hope someone can help sort this one out for me. Say you have to create some webcontrols dynamically. Each controls creation...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
1
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.