0

我正在 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

现在问题出在我的 Main.aspx 页面中,如果用户在用户控件中选择了任何复选框,我想将值保存在 NEXT 按钮导航单击事件的会话中。

我在 NEXT Button Click 中尝试了以下代码,但没有成功。

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
4

1 回答 1

0

这是艰难的道路。

这是简单的方法。

  1. 定义一个接口并在代表此操作的用户控件中实现它。这可能GetNamesOfSelectedValues是适当的或任何适当的。(从技术上讲,不需要接口,但我发现这是一种很好的做法,并且在许多情况下它可以实现“定义更明确的合同”。)
  2. 使用父页面代码中在所述子级上定义的接口。仅OnLoad子控件(在页面加载事件之后发生)之后使用它,否则控件将不会被恢复。

也就是说,父级不应该知道自定义用户控件中的任何控件。

这种方法效果很好。

于 2012-04-18T20:43:17.130 回答