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

Discussion in 'ASP .Net Web Controls' started by andres.leon@gmail.com, Oct 17, 2005.

  1. Guest

    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).controls.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).controls.item(0)
    intQID =
    Mid(tblDynamic.Controls.Item(1).Controls.Item(itemCounter).Controls.Item(0).ID,
    5, _

    Len(tblDynamic.Controls.Item(1).Controls.Item(0).Controls.Item(0).ID))
    bolActive =
    CType(tblDynamic.Controls.Item(1).Controls.Item(itemCounter).Controls.Item(0),
    CheckBox).Checked

    Next

    End Sub
     
    , Oct 17, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    4
    Views:
    13,206
    Teemu Keiski
    Nov 18, 2003
  2. =?Utf-8?B?dHJpbml0eXBldGU=?=

    must be placed inside a form tag with runat=server with inheritanc

    =?Utf-8?B?dHJpbml0eXBldGU=?=, Dec 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    7,759
    Steven Cheng[MSFT]
    Dec 22, 2004
  3. Replies:
    0
    Views:
    2,100
  4. Eliyahu Goldin
    Replies:
    7
    Views:
    285
    Ken Cox [Microsoft MVP]
    Jan 12, 2005
  5. Matthew Louden
    Replies:
    3
    Views:
    230
    Teemu Keiski
    Nov 18, 2003
Loading...

Share This Page