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

G

Guest

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["vStudyAText"].ToString).Trim();
}
txtAnswers.ID = "txtAnswers";
this.Controls.AddAt(this.Controls.IndexOf(PlaceHolder1), 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,
 
G

Guest

try and set the enableviewstate property to true for the dynamic text control.

With Regards
Shane Sukul
MCSD MCAD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top