dynamically adding textbox

asp.net Tirupati, India
  • 12 years ago
    Hi, the below code is for dynamically adding textbox, but it will adding for same textbox only my requirement is how many times i click add button such times textbox will add for ex: if i click 10 times add button 10 textboxs will add Session["TextBoxCount"] = "3"; for (int a = 0; a < int.Parse(Session["TextBoxCount"].ToString()); a++) { TableRow tr = new TableRow(); TableCell tc = new TableCell(); TextBox tb = new TextBox(); tb.Attributes.Add("runat", "Server"); tb.EnableViewState = false; tb.MaxLength = 128; tb.ID = "TextBox" + a; tc.Controls.Add(tb); tr.Cells.Add(tc); ctrlTable.Rows.Add(tr);
  • 12 years ago

    I'll just assume you don't know the difference betwen VB & VC#, which is not a good start. Could you also phrase your problem as a question too, you don't really give any indication anything is wrong; anyhoo:

    Your code doesn't seem to show any problems, the only thing you have not showed is how your displaying the table to your form. And I suppose here in lies the problem. Add a Placeholder object to the form and add the table to the Placeholder.

        Private table As New HtmlTable()
        Private cell As HtmlTableCell
        Private row As HtmlTableRow
        Private tb As TextBox
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            For i As Integer = 0 To 9
                tb = New TextBox()
                tb.Attributes.Add("runat", "server")
                tb.MaxLength = 100
                tb.ID = "TextBox" + i.ToString
                cell = New HtmlTableCell()
                cell.Controls.Add(tb)
                row = New HtmlTableRow()
                row.Cells.Add(cell)
                table.Rows.Add(row)
            Next
            Placeholder1.Controls.Add(table)
        End Sub
  • 12 years ago

    Joe Stagner (Microsoft) has posted a new video on just this subject...

    http://asp.net/learn/ajax-videos/video-286.aspx

    Cheers

    Tom

  • 12 years ago

    Appologies for my feculant comments, I thought I was still in VB.NET. I'm the murky scumbag.

  • 12 years ago

    [quote user="D'Scouser"]

    Appologies for my feculant comments, I thought I was still in VB.NET. I'm the murky scumbag.

    [/quote]

    Feculant..... good word. *noted*

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski