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

Reply
Thread Tools

"must be placed inside a form tag with runat=server" error when dynamically creating web controls

 
 
andres.leon@gmail.com
Guest
Posts: n/a
 
      10-17-2005
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:TableRow>
<asp:TableHeaderCell>Active</asp:TableHeaderCell>
<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>
</asp:TableRow>
</asp:table></P>
<P><asp:button id="btnAddQuestion" runat="server" Text="Add
Question"></asp:button></P>
<asp:button id="btnSumbmit" runat="server" Text="Update Survey
Data"></asp:button>
<P></P>

postback vb page:

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

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

cnn = New SqlConnection(strCnnString)
cmd = New SqlCommand(strSQLCommand, cnn)
cnn.Open()
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
Else
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
td0.Controls.Add(chkQuestion)
chkQuestion.Attributes.Add("runat", "server")
Dim td1 As TableCell = New TableCell
td1.Controls.Add(txtQtext)
Dim td2 As TableCell = New TableCell
td2.Controls.Add(txtA1Text)
Dim td3 As TableCell = New TableCell
td3.Controls.Add(txtA2Text)
Dim td4 As TableCell = New TableCell
td4.Controls.Add(txtA3Text)
Dim td5 As TableCell = New TableCell
td5.Controls.Add(txtA4Text)
Dim td6 As TableCell = New TableCell
td6.Controls.Add(txtA5Text)
tr.Cells.Add(td0)
tr.Cells.Add(td1)
tr.Cells.Add(td2)
tr.Cells.Add(td3)
tr.Cells.Add(td4)
tr.Cells.Add(td5)
tr.Cells.Add(td6)
tblDynamic.Rows.Add(tr)
'Page.Controls.Add(chkQuestion)
'Page.Controls.Add(txtQtext)
'Page.Controls.Add(txtA1Text)
'Page.Controls.Add(txtA2Text)
'Page.Controls.Add(txtA3Text)
'Page.Controls.Add(txtA4Text)
'Page.Controls.Add(txtA5Text)
End While



End If

cnn.Close()
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
String

'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),
CheckBox).Checked

Next

End Sub

 
Reply With Quote
 
 
 
Reply

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. davidr@sharpesoft.com 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



Advertisments