A
andres.leon
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
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