问题描述
我正在创建一个在线考试页面,其中包含30个在运行时动态创建的单选按钮。
我将如何获取每个单选按钮的“ 0”事件并将其标记在我的方法中,以检查下一个问题是否需要跳转或转义。
例:
如果我的问题是10,并且答案为“是”,请将我重定向到问题15,否则请转到下一个问题
解决方法
HTML代码
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head runat=\"server\">
<title></title>
</head>
<body>
<form id=\"form1\" runat=\"server\">
<asp:Panel ID=\"RadioButtonsPanel\" runat=\"server\" />
</form>
</body>
</html>
VB代码-
Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
\' Add each radio button
AddNewRaduiButton(\"MyRadio1\")
AddNewRaduiButton(\"MyRadio2\")
AddNewRaduiButton(\"MyRadio3\")
AddNewRaduiButton(\"MyRadio4\")
End Sub
Private Sub AddNewRaduiButton(ByVal name As String)
\' Create a new radio button
Dim MyRadioButton As New RadioButton
With MyRadioButton
.ID = name
.AutoPostBack = True
.Text = String.Format(\"Radio Button - \'{0}\'\",name)
End With
\' Add the click event to go to the sub \"MyRadioButton_CheckedChanged\"
AddHandler MyRadioButton.CheckedChanged,AddressOf MyRadioButton_CheckedChanged
Page.FindControl(\"RadioButtonsPanel\").Controls.Add(MyRadioButton)
End Sub
Protected Sub MyRadioButton_CheckedChanged(ByVal sender As Object,ByVal e As System.EventArgs)
\' Convert the Sender object into a radio button
Dim ClickedRadioButton As RadioButton = DirectCast(sender,RadioButton)
\' Display the radio button name
MsgBox(String.Format(\"Radio Button {0} has been Updated!\",ClickedRadioButton.ID))
End Sub
,使用以下语句:
AddHandler radioButton.Click,AddressOf instance.MethodName
请参阅如何:在ASP.NET网页中在运行时动态绑定事件处理程序
,还可以考虑使用匿名子项(仅VB2010)内联编写事件处理程序
AddHandler radioButton.Click,Sub(s As Object,e As EventArgs)
MessageBox.Show(\"Awesome!\")
End Sub
从这里改编
您也可以使用闭包...