我正在asp.net中构建一个向导。我有一个主aspx页面,它将具有NEXT和PREVIOUS按钮用于导航。单击每个NEXT或PREVIOUS将加载适当的用户控件。
在一个用户控件中,我必须创建动态复选框,如下所示:
Public Class ucQuestion
Inherits System.Web.UI.UserControl
#Region "UserControl Events"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not (Page.IsPostBack) Then
AddControls(0)
End If
Catch ex As Exception
Throw ex
End Try
End Sub
#End Region
Protected Friend Sub AddControls(ByVal iListQuestionCount As Integer)
Try
Dim oExam As New BusinessObject.Wizard.ExamVM
If (System.Web.HttpContext.Current.Session(BusinessLayer.Constants.WizardObjectCollection) IsNot Nothing) Then
oExam = DirectCast(System.Web.HttpContext.Current.Session(BusinessLayer.Constants.WizardObjectCollection), BusinessObject.ExamWizard.ExamVM)
End If
'divQuestion.Controls.Clear()
Dim ctrl As New Literal
ctrl.ID = "lit" + iListQuestionCount.ToString()
ctrl.Text = oExam.Wizard.ExamQuestions(iListQuestionCount).Label
divQuestion.Controls.Add(ctrl)
For iLoopChildCount As Integer = 0 To oExam.Wizard.ExamQuestions(iListQuestionCount).Choices.Count - 1
Dim childctrl As New CheckBox
'childctrl.AutoPostBack = True
childctrl.ID = "chk" + (iLoopChildCount + 1).ToString()
childctrl.Text = oExam.Wizard.ExamQuestions(iListQuestionCount).Choices(iLoopChildCount).Label
childctrl.Checked = oExam.Wizard.ExamQuestions(iListQuestionCount).Choices(iLoopChildCount).IsSelected
'AddHandler childctrl.CheckedChanged, AddressOf OnCheckedClick
divQuestion.Controls.Add(childctrl)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
NoW问题出现在我的Main.aspx页面中,如果用户在usercontrol中选中了任何复选框,我想在会话中保存会话,点击NEXT按钮导航点击事件。
我尝试在NEXT按钮单击下面的代码,但它不成功。
For iLoopChildCount As Integer = 0 To oExamQuestions(ListIndex).Choices.Count - 1
Dim ctrl As Control
If (ucQuestion1.FindControl("chk" + (iLoopChildCount + 1).ToString()) IsNot Nothing) Then
ctrl = DirectCast(ucQuestion1.FindControl("chk" + (iLoopChildCount + 1).ToString()), CheckBox)
If (DirectCast(ctrl, CheckBox).Checked) Then
oBallotQuestions(ListIndex).Choices(iLoopChildCount).IsSelected = True
End If
End If
Next
答案 0 :(得分:0)
这是艰难的方式。
这是简单的方法。
GetNamesOfSelectedValues
或任何合适的。 (从技术上讲,界面不是必需的,但我发现这是一种很好的做法,在很多情况下它会产生“更明确的合同”。)OnLoad
之后使用(在页面加载事件后发生),否则控件将无法恢复。也就是说,父不应该知道自定义用户控件中的任何控件。
这种方法效果很好。