【问题标题】:Accessing dynamically created checkbox in user control from container page从容器页面访问用户控件中动态创建的复选框
【发布时间】:2012-04-18 20:31:41
【问题描述】:

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

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    这很难。

    这是最简单的方法。

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

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

    这种方法效果很好。

    【讨论】:

    • 看不懂,能不能举个例子
    • 好吧,首先定义一个接口(见链接):父需要知道/访问什么?然后将类implements 设为该接口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多