Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Ok, I've 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.

=============================================

 

Here's my 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

 tags [/ vb][/color] [/edit]
Edited by Robby
3,450,897,223 posts away from crazy...
  • Moderators
Posted

You adding a Placeholder each time you loop. I don't know if you want the same thing for the Panel.

Consider placing these two lines before your loop...

Controls.Add(PlaceHolder1)
PlaceHolder1.Controls.Add(Panel1)

Visit...Bassic Software
Posted
Thanks Robby, that's a bit cleaner. But, I'm still in the same boat as before with the textboxes. Any clues?
3,450,897,223 posts away from crazy...
  • Moderators
Posted

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.

 

I'll try and take the time later to step through your code and see what's going on.

Visit...Bassic Software
Posted

I don't 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.

 

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 label's 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

Posted

J:

I'm 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.

3,450,897,223 posts away from crazy...
Posted

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.

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 can't be added dynamically, which would make some sense as it is only used to make coding simpler, not outputted.

 

John

Posted

John:

Thanks man!! It's 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!

3,450,897,223 posts away from crazy...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...