Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > "must be placed inside a form tag with runat=server" error when dynamically creating web controls

Thread Tools

"must be placed inside a form tag with runat=server" error when dynamically creating web controls
Posts: n/a
Hello All,
The following code is supposed to create a table web control that
contains checkboxes and textboxes in each cell based on an SQL query.

When i run the page and then request to see the result, i get the
textboxes and checkboxes in the table fine, however these form item are
not part of the page.controls collection, nor are they part of the
table tblDynamic collection either.

Can someone please shed some light on what i may be doing incorreclty
here? Thank you!

aspx page:

<P><asp:table id="tblDynamic" runat="server">
<asp:TableHeaderCell>Question Text</asp:TableHeaderCell>
<asp:TableHeaderCell>Answer 1 Text</asp:TableHeaderCell>
<asp:TableHeaderCell>Answer 2 Text</asp:TableHeaderCell>
<asp:TableHeaderCell>Answer 3 Text</asp:TableHeaderCell>
<asp:TableHeaderCell>Answer 4 Text</asp:TableHeaderCell>
<asp:TableHeaderCell>Answer 5 Text</asp:TableHeaderCell>
<P><asp:button id="btnAddQuestion" runat="server" Text="Add
<asp:button id="btnSumbmit" runat="server" Text="Update Survey

postback vb page:

Private Sub GetSurveyQuestions(ByVal intSurveyID As Integer)
Dim cnn As SqlConnection
Dim cmd As SqlCommand

Dim strSQLCommand As String = "SELECT * FROM tblITSurvey INNER
JOIN tblitsurveyquestion on " & _
" tblITSurvey.surveyid = tblitsurveyquestion.surveyid WHERE
tblITSurvey.SurveyID = " & _

cnn = New SqlConnection(strCnnString)
cmd = New SqlCommand(strSQLCommand, cnn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
While dr.Read
lblSurveyID.Text = CInt(dr("SurveyID"))
txtTitle.Text = CStr(dr("title"))
txtDescription.Text = CStr(dr("description"))
lblDateCreated.Text = Format(CDate(dr("datecreated")),
"M/d/yyyy H:mm")
Dim txtQtext, txtA1Text, txtA2Text, txtA3Text,
txtA4Text, txtA5Text As TextBox
Dim chkQuestion As CheckBox
Dim tr As TableRow = New TableRow
chkQuestion = New CheckBox

txtQtext = New TextBox
txtA1Text = New TextBox
txtA2Text = New TextBox
txtA3Text = New TextBox
txtA4Text = New TextBox
txtA5Text = New TextBox
chkQuestion.ID = "chkQ_" & CInt(dr("qid"))
txtQtext.ID = "txtQtext_" & CInt(dr("qid"))
txtA1Text.ID = "txtA1Text_" & CInt(dr("qid"))
txtA2Text.ID = "txtA2Text_" & CInt(dr("qid"))
txtA3Text.ID = "txtA3Text_" & CInt(dr("qid"))
txtA4Text.ID = "txtA4Text_" & CInt(dr("qid"))
txtA5Text.ID = "txtA5Text_" & CInt(dr("qid"))
If dr("qactive") = True Then
chkQuestion.Checked = True
chkQuestion.Checked = False
End If

txtQtext.Text = dr("qtext")
txtA1Text.Text = dr("a1text")
txtA2Text.Text = dr("a2text")
txtA3Text.Text = dr("a3text")
txtA4Text.Text = dr("a4text")
txtA5Text.Text = dr("a5text")
Dim td0 As TableCell = New TableCell
chkQuestion.Attributes.Add("runat", "server")
Dim td1 As TableCell = New TableCell
Dim td2 As TableCell = New TableCell
Dim td3 As TableCell = New TableCell
Dim td4 As TableCell = New TableCell
Dim td5 As TableCell = New TableCell
Dim td6 As TableCell = New TableCell
End While

End If

End Sub

Private Sub btnSumbmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSumbmit.Click
'all the content we need to worry about is in the tblDynamic
table. So:
'tblDynamic.Controls.item(1) refers to the first dynamically
create row
'tblDynamic.Controls.item(1).controls.item(0) refers to the
first cell in the row
'tblDynamic.Controls.item(1).controls.item(0).cont rols.item(0)
refers to the first control in the cell
'each cell will have one and only one control

Dim itemCounter As Integer
For itemCounter = 0 To 6
Dim bolActive As Boolean
Dim intQID As Integer
Dim txtQuestion, txtA1, txtA2, txtA3, txtA4, txtA5 As

'tblDynamic.Controls.Item(1).Controls.item(0).cont rols.item(0)
intQID =
Mid(tblDynamic.Controls.Item(1).Controls.Item(item Counter).Controls.Item(0).ID,
5, _

Len(tblDynamic.Controls.Item(1).Controls.Item(0).C ontrols.Item(0).ID))
bolActive =
CType(tblDynamic.Controls.Item(1).Controls.Item(it emCounter).Controls.Item(0),


End Sub

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Dynamic User Control Error: "The control must be placed inside a form tag with runat=server" Help Please Second time posting. ASP .Net 0 08-31-2006 05:26 PM
error..of type 'TextBox' must be placed inside a form tag with runat=server Eliyahu Goldin ASP .Net Datagrid Control 7 01-12-2005 02:21 AM
Control must be placed inside a form tag John ASP .Net 2 11-03-2004 07:58 AM
Run-time error: <Control Name> must be placed inside a form tag with runat=server Matthew Louden ASP .Net 4 11-18-2003 06:16 PM
Run-time error: <Control Name> must be placed inside a form tag with runat=server Matthew Louden ASP .Net Web Controls 3 11-18-2003 06:16 PM