对使用 ViewState 和动态添加的用户控件感到困惑

发布于 2024-12-05 19:31:53 字数 2247 浏览 5 评论 0原文

在我的网页上,我正在加载用户控件的多个实例,有时用户控件本身会加载。我需要为回发的往返保存一堆属性,但我对如何将这些属性保存到 ViewState 并将它们再次设置为用户控件中的转发器项目感到困惑。

在这种情况下任何人都可以帮助我吗,我已经阅读了关于 Viewstate 的 MSDN,但由于某种原因我不太理解它

这就是我加载父用户控件的方式(子控件使用相同的用户控件以相同的方式加载)

Protected Sub Load_Controls(ByVal list As List(Of BSEvaluationGroup.category), ByVal gid As Integer, ByVal pid As Integer, ByVal fid As Integer)
    Dim item As BSEvaluationGroup.category
    For Each item In list
        Dim ctl As PerformanceEvaluationSubcontractorControl = CType(Page.LoadControl("PerformanceEvaluationSubcontractorControl.ascx"), PerformanceEvaluationSubcontractorControl)
        ctl.categoryid = item.catid
        ctl.categoryname = item.catname
        ctl.projectid = pid
        ctl.folderid = fid
        ctl.groupid = gid
        ctl.parentid = item.parid
        ctl.clist = item.categories
        ctl.plist = item.points
        ctl.parentpage = Me
        ctl.EnableViewState = "true"
        If (Not subcon Is Nothing AndAlso Not subcon.points Is Nothing) Then
            ctl.epnts = subcon.points
        End If
        AddHandler ctl.BubbleCalculate, AddressOf Me.PostRating

        Select Case gid
            Case 1
                Me.officephld.Controls.Add(ctl)
                Dim ohrule As HtmlGenericControl = New HtmlGenericControl("hr")
                ohrule.Style.Add("width", "100%")
                ohrule.Style.Add("background-color", "Silver")
                ohrule.Style.Add("size", "1px")
                ohrule.Style.Add("border-width", "0")
                ohrule.Style.Add("padding-top", "1px")
                ohrule.Style.Add("float", "left")
                Me.officephld.Controls.Add(ohrule)
            Case 2
                Me.sitephld.Controls.Add(ctl)
                Dim shrule As HtmlGenericControl = New HtmlGenericControl("hr")
                shrule.Style.Add("width", "100%")
                shrule.Style.Add("background-color", "Silver")
                shrule.Style.Add("size", "1px")
                shrule.Style.Add("border-width", "0")
                shrule.Style.Add("padding-top", "1px")
                shrule.Style.Add("float", "left")
                Me.sitephld.Controls.Add(shrule)
        End Select
    Next
End Sub

On my webpage I am loading multiple instances of a usercontrol, sometimes the usercontrol is laoded within itself. I need to save a bunch of properties for the round trip of a post back but i am confused on how to save those properties to ViewState and set them again to the repeater items within the usercontrol.

Can anyone help me in this situation, I have read the MSDN on Viewstate but I am not understanding it quite well for some reason

This is how I load the parent user controls (child controls are loaded the same way with the same user control)

Protected Sub Load_Controls(ByVal list As List(Of BSEvaluationGroup.category), ByVal gid As Integer, ByVal pid As Integer, ByVal fid As Integer)
    Dim item As BSEvaluationGroup.category
    For Each item In list
        Dim ctl As PerformanceEvaluationSubcontractorControl = CType(Page.LoadControl("PerformanceEvaluationSubcontractorControl.ascx"), PerformanceEvaluationSubcontractorControl)
        ctl.categoryid = item.catid
        ctl.categoryname = item.catname
        ctl.projectid = pid
        ctl.folderid = fid
        ctl.groupid = gid
        ctl.parentid = item.parid
        ctl.clist = item.categories
        ctl.plist = item.points
        ctl.parentpage = Me
        ctl.EnableViewState = "true"
        If (Not subcon Is Nothing AndAlso Not subcon.points Is Nothing) Then
            ctl.epnts = subcon.points
        End If
        AddHandler ctl.BubbleCalculate, AddressOf Me.PostRating

        Select Case gid
            Case 1
                Me.officephld.Controls.Add(ctl)
                Dim ohrule As HtmlGenericControl = New HtmlGenericControl("hr")
                ohrule.Style.Add("width", "100%")
                ohrule.Style.Add("background-color", "Silver")
                ohrule.Style.Add("size", "1px")
                ohrule.Style.Add("border-width", "0")
                ohrule.Style.Add("padding-top", "1px")
                ohrule.Style.Add("float", "left")
                Me.officephld.Controls.Add(ohrule)
            Case 2
                Me.sitephld.Controls.Add(ctl)
                Dim shrule As HtmlGenericControl = New HtmlGenericControl("hr")
                shrule.Style.Add("width", "100%")
                shrule.Style.Add("background-color", "Silver")
                shrule.Style.Add("size", "1px")
                shrule.Style.Add("border-width", "0")
                shrule.Style.Add("padding-top", "1px")
                shrule.Style.Add("float", "left")
                Me.sitephld.Controls.Add(shrule)
        End Select
    Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-12-12 19:31:53

访问视图状态很简单,例如 ViewState("PropertyName")。视图状态包特定于控件实例,因此您可以在多个控件类型和实例中使用相同的属性名称。

这里唯一重要的是 ASp.NET 运行时必须匹配视图状态包来控制实例,为此它使用 ID 属性(该属性在父命名容器中是唯一的)。因此,为动态用户控件实例分配唯一的 ID(并在回发时维护相同的控制树层次结构和 id - 本质上这意味着在回发时执行相同的代码并且不使用随机 id)非常重要。所以你的代码应该是这样的

...
Dim n As Integer
n = 1
For Each item In list
        Dim ctl As PerformanceEvaluationSubcontractorControl = CType(Page.LoadControl("PerformanceEvaluationSubcontractorControl.ascx"), PerformanceEvaluationSubcontractorControl)
        ctl.ID = "MyCtl" & n.ToString()
        ctl.categoryid = item.catid
....

Accessing view-state is simple such as ViewState("PropertyName"). The View State bag is specific to a control instance so you can use same property name within multiple control types and instances.

Only important thing here is that ASp.NET run-time has to match view-state bags to control instances and for that it uses ID property (which is unique within the parent naming container). So its important that you assign unique IDs to your dynamic user control instances ( and maintain same control tree hierarchy and ids on postback - essentially it means that execute the same code on postback and don't use random ids). So your code should be something like

...
Dim n As Integer
n = 1
For Each item In list
        Dim ctl As PerformanceEvaluationSubcontractorControl = CType(Page.LoadControl("PerformanceEvaluationSubcontractorControl.ascx"), PerformanceEvaluationSubcontractorControl)
        ctl.ID = "MyCtl" & n.ToString()
        ctl.categoryid = item.catid
....
青巷忧颜 2024-12-12 19:31:53

这是一个控件 ID 问题,我将其删除而不是添加 ID

It was a control ID issue, I removed it instead of adding an ID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文