1

早上好堆栈溢出!

我有一个小问题,我正在努力解决这件事,这让我的生活变得糟透了!

在我的 .aspx 页面上,我希望能够根据用户选择(单选按钮列表)显示和隐藏某些面板。

例如,在我的 aspx 页面中,我有;

<form id="form1" runat="server">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:RadioButtonList>

    <asp:Panel ID="Panel1" runat="server" Width="50%">
        Visible or not visible depending on radio choice<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </asp:Panel>
    </form>

然后在我的 aspx.vb 中有;

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If RadioButtonList1.SelectedItem.Equals(Nothing) Then
            Panel1.Visible = False
        Else
            RadioButtonList1.SelectedItem.Equals(3)
            Panel1.Visible = True
        End If

    End Sub

我还尝试了此代码的一些不同变体,以及选择语句。如果有人可以就如何解决这个问题提供任何建议,将不胜感激

非常感谢,菲尔

编辑:

经过进一步的尝试和对 msdn 的一些阅读,我现在有了;

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



' Show or Hide the Panel contents.
    If RadioButtonList1.SelectedItem.Equals(3) Then
        Panel1.Visible = True
    Else
        Panel1.Visible = False
    End If

End Sub

但是当我尝试运行我得到的代码时;

此行上的“对象引用未设置为对象的实例” If RadioButtonList1.SelectedItem.Equals(3) Then

4

2 回答 2

1

发生这种情况有几个原因。首先,没有选定的项目,所以当您尝试执行“RadioButtonList1.SelectedItem.Equals(3)”时,SelectedItem 是 Nothing,因此没有对象可以执行 Equals 比较。

接下来,您将尝试查看 SelectedItem 是否等于 3。 SelectedItem 将是ListItem 对象。您想比较该对象的 Value 属性: RadioButtonList1.SelectedItem.Value

最后,由于 RadioButtonList1.SelectedItem.Value 返回一个字符串,因此 .Equals 永远不会为真,因为您要询问数字 3 是否与字符串“3”相同。

要修复它,请检查是否存在选定值,然后将 RadioButtonList1.SelectedItem.Value 与字符串“3”进行比较:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Set the panel to hidden by default
    Panel1.Visible = False

    ' Check to see if there's a selected value
    If Not RadioButtonList1.SelectedItem Is Nothing Then
        ' there is.. check to see if the value is correct
        If RadioButtonList1.SelectedItem.Value = "3" Then
            ' it is.. show the panel!
            Panel1.Visible = True
        End If
    End If

End Sub
于 2010-02-04T09:17:49.470 回答
0
panel.enabled = false

可能会成功,否则你总是可以尝试使用 javascript 或 jquery 或类似的东西来设置

display = none

或调用(使用 jquery)

$('#Panel1').hide();
于 2010-02-04T07:34:56.957 回答