views:

255

answers:

3

How can i dynamically create textboxes that depends on database record count in asp.net? For example: there is a school program that you will be able to insert exam points of students and also there are some classes in school. You don't know how many people there is and you should dynamically create textboxes to insert exam points.

+2  A: 

The page control, the panel control, and several others have a .Controls object in which you can dynamically add new controls to the page. It then becomes something as simple as:

Dim txtDynamic As New Textbox()
Me.Page.Controls.Add(txtDynamic)

You may want to apply additional properties to the controls, in which check out this MSDN article with gives the full details on adding controls.

Dillie-O
Thanks Dillie, I think that's what i am looking for:) Thanks again.
Mehmet Kaleli
A: 

Depending on the nature of the data you need to process in the TextBox, you could easily have an HTML literal control on the page as a placeholder. Then in your code behind page, you simply add the proper HTML code to create the number of TextBoxes needed and set the resulting code to your HTML literal control. If you needed to get at the data, you can simply add a "runat=server" attribute to all of your standard HTML textbox controls.

This approach gives you a little more immediate flexibility on how the textboxes will look without having to setup a DataList or something to that nature.

Dillie-O
I realize I've posted two answers here, but I figure this will "poll" to see which approach will work best and is preferred by the group.
Dillie-O
+1  A: 

+1 to Dillie for the correct answer.

I'd like to add that if you want to do this you'd better have a very good understanding of the ASP.NET page lifecycle. It is extremely important you create your controls at the correct time, otherwise they will not be available for the page to fire events on/fill with data from the postback. Generally, this should be during the Page_Init event (tho its been awhile since I've done this!).

Will
+1 for the page lifecycle (and thanks for the props 8^D) It is VERY important to make sure when you're creating/processing these controls as Will is explaining.
Dillie-O