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

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.