I am dynamically creating a table which places a dynamically created radiobuttonlist in each row.
I then want to display the results on the page when the submit button is clicked.
I am getting an error message stating that
'question1' is not declared
Why might this be happening?? I've included all my code below.
Code behind:
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
For i As Integer = 1 To 3
Dim TableRow As New TableRow()
Dim TableRowCell_1 As New TableCell()
TableRowCell_1.Text = i
Dim TableRowCell_2 As New TableCell()
TableRow.Cells.Add(TableRowCell_1)
TableRow.Cells.Add(TableRowCell_2)
QuestionnaireTable.Rows.Add(TableRow)
Dim question As New RadioButtonList
question.ID = "question" & i
question.RepeatColumns = "2"
question.Items.Insert(0, new listitem("", "1"))
question.Items.Insert(1, new listitem("", "2"))
TableRowCell_3.Controls.Add(question)
Next
End Sub
Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
Dim question1 As RadioButtonList = DirectCast(Page.FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Page.FindControl("question2"), RadioButtonList)
Dim question3 As RadioButtonList = DirectCast(Page.FindControl("question3"), RadioButtonList)
Response.write(question1.SelectedValue & " - " & question2.SelectedValue & " - " & question3.SelectedValue)
End Sub
Body:
<asp:Table runat="server" ID="QuestionnaireTable" />
<asp:Button OnClick="btnSendFeedback_Click" runat="server" Text="Submit..." ID="submitbutton" />
When you declare your controls in the markup, Visual Studio and ASP.NET make it so that you can access that control in the code-behind.
When you declare a control dynamically like this, you have to make accessible from the code-behind.
There are multiple ways to do this, probably the easiest is:
Dim question1 As RadioButtonList = DirectCast(Page.FindControl("question1"), RadioButtonList)