我试图在Visual 2012中创建一个测试网页,并在表中动态创建的无线电按钮列表中获取所选值时遇到了一些困难。
这就是我如何创建无线电按钮列表(并将其添加到表中)。
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option" & intQuestion.ToString
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
这就是在用户点击按钮后,我试图从无线电按钮列表中获取所选值的方法。
Dim rbl As RadioButtonList = DirectCast(Page.FindControl("Option" & intQuestion.ToString), RadioButtonList)
If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then
intScore += 1
End If
在这两种情况下,变量intQuestion = 0。在运行程序时,我遇到了以下错误:
对象引用未设置为对象的实例。
在这条线上:
如果rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString那么
我认为这显然意味着程序找不到它必须放在rbl中的控制。我在网上找不到任何答案。
有人能指出我的正确方向吗?
发布于 2015-05-18 07:17:35
这是我的.aspx代码。
<asp:Table ID="Questions" runat="server">
</asp:Table>
<asp:Button ID="SaveButton" runat="server" Text="Save" />
这是我的文件代码后面的代码。我增加了动态下拉列表。单击“保存”按钮,我将检索选定的值。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim NewRow As New TableRow
Dim NewCell As New TableCell
Dim rblOptions As New RadioButtonList
rblOptions.ID = "Option1"
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("1", "1"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("2", "2"))
rblOptions.Items.Add(New System.Web.UI.WebControls.ListItem("3", "3"))
NewCell.Controls.Add(rblOptions)
NewRow.Cells.Add(NewCell)
'Questions is a table
Questions.Rows.Add(NewRow)
End Sub
Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
If Page.IsValid Then
Dim rbl As RadioButtonList = DirectCast(Questions.FindControl("Option1"), RadioButtonList)
If rbl.SelectedValue.ToString = "ExpectedValue" Then
End If
End If
End Sub
https://stackoverflow.com/questions/30288548
复制相似问题