在下不才,写了一个玩意如下:
<%@ page language="vb" %>
<head>
<script runat="server"> dim myRadios as New RadioButtonList
dim myRadioItems as New ListItemCollection Sub page_load()
if not ispostback Then
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(0))
myitem.text = "Two"
myitem.value = "2"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(1)) PH.Controls.Add(myRadios)
Else
myLabel.text = myRadios.SelectedItem.Value
End If
End Sub
</script><html>
<body>
<form runat="server">
<asp:placeholder id="PH" runat="server" />
<br /><br />
<asp:label id="myLabel" runat="server" />
<br /><br />
<asp:button id="btnClick" text="提交" runat="server" />
</form>
</body>
顺利编译运行, 但有以下问题:
1:页面上的两个RadioButton的文本都是2, 不是我想要的1和2一起显示.
2.选中任何一个RadioButton,按了提交以后,就会报错,说未将对象引用设置到对象的实例。并给出了出错的地方:
行 23:
行 24: Else
行 25: ******(出错) myLabel.text = myRadios.SelectedItem.Value
行 26: End If
行 27: End Sub我现在没办法了,请大家帮我解决一下这两个毛病.
<%@ page language="vb" %>
<head>
<script runat="server"> dim myRadios as New RadioButtonList
dim myRadioItems as New ListItemCollection Sub page_load()
if not ispostback Then
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(0))
myitem.text = "Two"
myitem.value = "2"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(1)) PH.Controls.Add(myRadios)
Else
myLabel.text = myRadios.SelectedItem.Value
End If
End Sub
</script><html>
<body>
<form runat="server">
<asp:placeholder id="PH" runat="server" />
<br /><br />
<asp:label id="myLabel" runat="server" />
<br /><br />
<asp:button id="btnClick" text="提交" runat="server" />
</form>
</body>
顺利编译运行, 但有以下问题:
1:页面上的两个RadioButton的文本都是2, 不是我想要的1和2一起显示.
2.选中任何一个RadioButton,按了提交以后,就会报错,说未将对象引用设置到对象的实例。并给出了出错的地方:
行 23:
行 24: Else
行 25: ******(出错) myLabel.text = myRadios.SelectedItem.Value
行 26: End If
行 27: End Sub我现在没办法了,请大家帮我解决一下这两个毛病.
dim myRadioItems as New ListItemCollection Sub page_load()
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(0))
myitem.text = "Two"
myitem.value = "2"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(1)) PH.Controls.Add(myRadios)
if ispostback Then
myLabel.text = myRadios.SelectedItem.Value
End If
End Sub
</script>
myitem New ListItem()
myitem.text = "first"
myitem.value = "1"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(0))
myitem New ListItem()
myitem.text = "Two"
myitem.value = "2"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(1)) PH.Controls.Add(myRadios)
Dim myRadios as New RadioButtonList
Dim fItem as New ListItem
fItem.Text = "First"
fItem.Value = 1
Dim sItem as New ListItem
sItem.Text = "Second"
sItem.Value = 2
myRadios.Items.Add(fItem)
myRadios.Items.Add(sItem)
PH.Controls.Add(myRadios)
If IsPostBack Then
myLabel.text = myRadios.SelectedItem.Value
End If
帮我解决一下, 在下不胜感激!
Sub page_load()
if not ispostback Then
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadios.Items.Add(myitem)
myitem = New ListItem
myitem.text = "Two"
myitem.value = "2"
myRadios.Items.Add(myitem) end if
PH.Controls.Add(myRadios)
myLabel.text = myRadios.SelectedItem.Value
End Sub
<head>
<script runat="server"> dim myRadios as New RadioButtonList
Sub page_load()
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadios.Items.Add(myitem)
myitem = New ListItem
myitem.text = "Two"
myitem.value = "2"
myRadios.Items.Add(myitem) PH.Controls.Add(myRadios)
End Sub sub GetValue(ByVal sender as Object, ByVal e as EventArgs)
if myRadios.SelectedIndex >=0 then
myLabel.text = myRadios.SelectedItem.Value
end if
end sub
</script><html>
<body>
<form runat="server">
<asp:placeholder id="PH" runat="server" />
<br /><br />
<asp:label id="myLabel" runat="server" />
<br /><br />
<asp:button id="btnClick" text="提交" runat="server" OnClick="GetValue" />
</form>
</body>
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadios.Items.Add(myitem)
myitem = New ListItem
myitem.text = "Two"
myitem.value = "2"
myRadios.Items.Add(myitem)
End If
End Sub
dim myRadioItems as New ListItemCollection Sub page_load()
dim myitem as New ListItem
myitem.text = "first"
myitem.value = "1"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(0))
myitem.text = "Two"
myitem.value = "2"
myRadioItems.Add(myitem)
myRadios.Items.Add(myRadioItems(1)) PH.Controls.Add(myRadios)
if ispostback Then
myLabel.text = myRadios.SelectedItem.Value
End If
End Sub
</script>
PH.Controls.Add(myRadios) 这一句放在page_load()的第一行,而没有去重新初始化myRadios,却能正确运行,是什么道理呢? 这两个方法为什么要写上一个GetValue函数才能有效果呢?
遛鎏的我试了, 仍然会出现错误. 那么出现错误又是什么道理呢?
RadioButtonList uses ViewState to save the added items, but if you add those items before TrackViewState() is called, those added items will never be saved to the ViewState. TrackViewState is called when you call Controls.Add(....)2. since you called Controls.Add in the Page_Load, the posted back data (which radio is selected?) is available after Page_Load, but before any postback event like Button's Click event