更改 asp.net 页面中单选按钮列表选择的面板可见性属性
早上好,堆栈溢出!
我有一个小问题正在努力解决,这个问题困扰着我!
在我的 .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
我还尝试了此代码的几种不同变体,以及尝试选择语句。如果有人可以就如何解决这个问题提供任何建议,我将不胜感激,
非常感谢, Phil
编辑:
经过进一步的尝试和在 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
Good morning stackoverflow!
I have a little problem I'm trying to work out thats bugging the life out of me!
On my .aspx page i want to be able to show and hide certain panels depending on user selections (radiobuttonlists).
For example in my aspx page i have;
<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>
Then in my aspx.vb i have;
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
I've also tried a few different variants of this code, along with trying a select statement. If anyone could offer any advise on how to work this one out it greatly appreciate it
Thanks a lot,
Phil
EDIT:
After further attempts and some reading on msdn I now have;
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
But when I try to run the code I get;
"Object reference not set to an instance of an object" on this line If RadioButtonList1.SelectedItem.Equals(3) Then
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况有几个原因。首先,没有选定的项目,因此当您尝试执行“RadioButtonList1.SelectedItem.Equals(3)”时,SelectedItem 为 Nothing,因此没有对象可以执行 Equals 比较。
接下来,您尝试查看 SelectedItem 是否等于 3。SelectedItem 将是 ListItem 对象。您想要比较该对象的 Value 属性: RadioButtonList1.SelectedItem.Value
最后,由于 RadioButtonList1.SelectedItem.Value 返回一个字符串,因此 .Equals 永远不会为 true,因为您询问数字 3 是否与字符串相同“3”。
要修复此问题,请进行检查以查看是否存在选定值,然后将 RadioButtonList1.SelectedItem.Value 与字符串“3”进行比较:
You've got a few reasons that's happening. First, there's no selected item, so when you're trying to do "RadioButtonList1.SelectedItem.Equals(3)", SelectedItem is Nothing, so there's no object to perform the Equals comparison.
Next, you're trying to see if the SelectedItem is equal to 3. The SelectedItem will be a ListItem object. You want to compare the Value property of that object: RadioButtonList1.SelectedItem.Value
Last, since RadioButtonList1.SelectedItem.Value returns a string, that .Equals will never be true because you're asking if the number 3 is the same as the string "3".
To fix it, throw in a check to see if there is a selected value and then compare the RadioButtonList1.SelectedItem.Value to the string "3":
可能会成功,否则你总是可以尝试使用 javascript 或 jquery 或类似的东西来设置
或调用(使用 jquery)
might do the trick, otherwise you can always try to use javascript or jquery or something like that to either set
or call (with jquery)