Dynamically adding a textbox to a webform

Daspoo

Well-known member
Joined
Jan 27, 2003
Messages
99
Location
Fort Walton Beach, Florida
Ok, Ive got a good question (I think..heh)... I am trying to dynamically add textboxes and labels to a webform at runtime, and so far the labels are working perfectly. The textboxes, however, do not work. When I run the project (VB/ASP.NET web App, BTW), I get the following error message:

=============================================
Server Error in /WebAppCrystalRevisit Application.
--------------------------------------------------------------------------------

Control txtParams2 of type TextBox must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control txtParams2 of type TextBox must be placed inside a form tag with runat=server.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
=============================================

Heres my code:
Code:
For j = 0 To thisOne.ParamCount - 1
     Labels...
      myLabel(j) = New Label()
      myLabel(j).Text = Trim(StrConv(thisOne.ParamPrompt(j), VbStrConv.ProperCase))
      myLabel(j).ID = "Label" & (j + 2).ToString
      myLabel(j).Visible = True
      Controls.Add(PlaceHolder1)
      PlaceHolder1.Controls.Add(Panel1)
      Panel1.Controls.Add(myLabel(j))
      Textboxes...
      txtParams(j) = New TextBox()
      txtParams(j).ID = "txtParams" & (j + 2).ToString
      Controls.Add(PlaceHolder1)
      PlaceHolder1.Controls.Add(Panel1)
      Panel1.Controls.Add(txtParams(j))
       Add a spacer in the form of an HTML <p> element
      Dim spacer As LiteralControl = New LiteralControl("<p>")
      Panel1.Controls.Add(spacer)
      Panel1.Controls.Add(spacer)
      Panel1.Controls.Add(spacer)
Next
=============================================

How do I add this "runat=server" line item from within VB code? Any help would be greatly appreciated!! Thanks in advance! :D

[edit]Please use
Code:
 tags [/ vb][/COLOR] [/edit]
 
Last edited by a moderator:
You adding a Placeholder each time you loop. I dont know if you want the same thing for the Panel.
Consider placing these two lines before your loop...
Code:
Controls.Add(PlaceHolder1)
PlaceHolder1.Controls.Add(Panel1)
 
Regarding the message "TextBox must be placed inside a form tag with runat=server."

Your asp controls are runtat server, this error is because some of your controls are being placed outside the main form.

Ill try and take the time later to step through your code and see whats going on.
 
I dont quite understand why you are initialising the labels and textboxes as an array. If you just create a new object each time it works fine.

Code:
Dim i As Integer
Dim numlabels As Integer
 Get the number of labels to create.
numlabels = CInt(DropDownList1.SelectedItem.Text)
For i = 1 To numlabels
    Dim myLabel As Label = New Label()
     Set the labels Text and ID properties.
    myLabel.Text = "Label " & i
    myLabel.ID = "Label" & i
    PlaceHolder1.Controls.Add(myLabel)

     Add a textbox
    Dim myTextBox As TextBox = New TextBox()
    myTextBox.ID = "TextBox" & i
    myTextBox.Text = "TextBox" & i
    PlaceHolder1.Controls.Add(myTextBox)

     Add a spacer in the form of an HTML <BR> element
    Dim spacer As LiteralControl = New LiteralControl("<br>")
    PlaceHolder1.Controls.Add(spacer)
Next
 
J:
Im creating an array in order to use it to tie back to the parameters for a Crystal Viewer. Besides that, though, whether I do it the way you suggested, or if I do it the way my code is already prepared, I still get the error stated in the first post. Thanks for the assistance, though.
 
Having played around with your code for a while the problem is caused by the fact you are adding the PlaceHolder control from code, if you just drop this onto the form at the start then it all works fine.

The following worked for an autoposting dropdownlist, with a PlaceHolder already on the page.
Code:
Dim j As Integer
Dim numLabels As Integer = CInt(DropDownList1.SelectedItem.Text)

 Create array objects
Dim myLabel(numLabels) As Label
Dim txtParams(numLabels) As TextBox
Dim Panel1 As System.Web.UI.WebControls.Panel = New Panel()

PlaceHolder1.Controls.Add(Panel1)

For j = 1 To numLabels
        Labels...
        myLabel(j) = New Label()
        myLabel(j).Text = "Label" & (j + 2).ToString
        myLabel(j).ID = "Label" & (j + 2).ToString
        myLabel(j).Visible = True
        Panel1.Controls.Add(myLabel(j))

        Textboxes...
        txtParams(j) = New TextBox()
        txtParams(j).ID = "txtParams" & (j + 2).ToString
        Panel1.Controls.Add(txtParams(j))

         Add a spacer in the form of an HTML <p> element
        Dim spacer As LiteralControl = New LiteralControl("<p>")
        Panel1.Controls.Add(spacer)
        Panel1.Controls.Add(spacer)
        Panel1.Controls.Add(spacer)
    Next
End Sub

Leads me to believe that a PlaceHolder control cant be added dynamically, which would make some sense as it is only used to make coding simpler, not outputted.

John
 
John:
Thanks man!! Its unbelievable how ONE line of code can screw ya for an entire day. :) Thanks for the time you took in helping me to figure this out...I appreciate it a LOT. Have a good one!
 
Im an idiot for not picking up on it, for sure you cant create the placeHolder at runtime, I assumed that it was done at design time.
 
Back
Top