简体   繁体   中英

Confused about using ViewState with dynamically added usercontrol

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

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
....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM