-1

i am developing a mini project in asp.net-c#...in that i have one confusion...how can i visible number of textboxes at runtime to insert records in sql server 2005.

for example...

if i wants 3 textbox at runtime then we can do it easily by putting at the web forms...

but when we retrieves the record from database we dont know the exact number of records to insert into database....

i need to create textboxes dynamically as user specifies the no...if user specifies no of rows to 3, then 3 textboxes should be added onto the web form and also i need to read the text entered later (in my case its marks of the student)....

what is the best method to do it so ? ? thanks

mack28
  • 129
  • 2
  • 7
  • 20

3 Answers3

0

try something like this on Page_Init

 for (int i = 0; i < ANYNUMBER; i++) 
       {
                var textBox = new TextBox();
                textBox.ID = "txb_" + i.ToString();                    
                this.Controls.Add(textBox);
       }

Also you can have a look at Dynamically Create Controls in ASP.NET

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
0

Use a repeater. http://msdn.microsoft.com/en-us/library/c012haty(v=vs.90).aspx

Here's an example (in VB.NET) that uses a Repeater without a real datasource.

VB.NET Repeater Simple Data Binding Without Datasource

Here it is in C#

myRepeater.DataSource = new int[3];
myRepeater.DataBind();

<asp:Repeater id="myRepeater" runat="server">
<ItemTemplate>
  <input type="text" />
</ItemTemplate>
</asp:Repeater>
Community
  • 1
  • 1
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
0

You have many options here. Unfortunately, I was unable to get a sense of which approach makes the most sense for you because I wasn't able to tell how the additional controls were used.

But one option is to allocate a new instance of the control you want and add it to the Controls collection of the parent control.

Another approach is to use a repeater, grid or other data control. Use this approach if the number of controls depends on the number of data items you need to display.

Still another approach is to insert text into, for example, a Literal control with the HTML markup you need. If you don't need the values to post back to the server, this is a leaner way of doing it, although it may be a bit more work.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • give me the example of gridview or repeater if u have tested ?? i think its the easy way from which i can insert records into database... – mack28 May 03 '12 at 10:38
  • You really haven't provided enough detail about what you are doing for me to supply an example. There are examples of using a repeater control all over the web if you need a general example. – Jonathan Wood May 03 '12 at 10:39
  • you can see this link http://stackoverflow.com/q/10425871/1357042 what exactly i want to do is... – mack28 May 03 '12 at 10:44