SelectedValue 来自动态创建的 radiobuttonlist vb [英] SelectedValue from dynamically created radiobuttonlist vb

查看:26
本文介绍了SelectedValue 来自动态创建的 radiobuttonlist vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Visual Studio 2012 中创建一个测验网页,但在从表格内动态创建的单选按钮列表中获取所选值时遇到了一些麻烦.

I'm trying to create a quiz webpage in Visual Studio 2012 and have a little trouble getting the selected value from a dynamically created radiobuttonlist inside a table.

这就是我创建单选按钮列表的方式(并将其添加到表格中)

This is how I create the radiobuttonlist (and add it to the table)

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)

这就是我在用户点击按钮后尝试从单选按钮列表中获取 selectedvalue 的方式.

This is how I am trying to get the selectedvalue from the radiobuttonlist after the users hits a button.

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.运行程序时遇到错误:

In both cases the variable intQuestion = 0. When running the program I come across the error:

未将对象引用设置为对象的实例.

Object reference not set to an instance of an object.

在这一行:

如果 rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString 那么

If rbl.SelectedValue.ToString = sdrGetQuestions.Item(0).ToString Then

我认为这显然意味着程序找不到它必须放在 rbl 中的控件.我在网上找不到任何答案...

Which I think obviously means the program couldn't find the control it had to place in rbl. I'm couldn't find any answers online...

有人能指出我正确的方向吗?

Could anyone point me in the right direction?

推荐答案

这是我的 .aspx 代码.

This is my .aspx code.

<asp:Table ID="Questions" runat="server">

</asp:Table>
<asp:Button ID="SaveButton" runat="server" Text="Save" />

这是我的文件代码背后的代码.我添加了动态下拉列表.单击保存按钮时,我正在检索选定的值.

This is my code behind file code. I have added dynamic drop-down list. On save button click, I am retrieving selected value.

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

这篇关于SelectedValue 来自动态创建的 radiobuttonlist vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆