简体   繁体   English

ASP.NET为自定义控件模板内的控件维护ViewState

[英]ASP.NET Maintaining ViewState for controls inside a custom control template

I have a template property in my control declared as follows: 我的控件中有一个模板属性,声明如下:

<TemplateContainer(GetType(GenericTemplateContainer)),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateInstance(TemplateInstance.Single)>
Property CustomTemplate As ITemplate

In my control's Init event I have the following: 在控件的Init事件中,我具有以下内容:

If Me.CustomTemplate IsNot Nothing Then
    Dim TemplateContainer As New GenericTemplateContainer
    Me.CustomTemplate.InstantiateIn(TemplateContainer)

    PlaceHolder.Controls.Add(TemplateContainer)
End If

This allows me to place controls in markup inside my template, but on a post back the controls inside the template are not holding their ViewState. 这使我可以将控件放置在模板内部的标记中,但是在回发时,模板内部的控件未保持其ViewState。

I have tried adding PersistChildren(True) attribute to CustomTemplate property but I cannot because it's not valid. 我尝试将PersistChildren(True)属性添加到CustomTemplate属性,但是因为它无效而无法执行。

Are you putting the values into ViewState? 您是否将值放入ViewState? From what I understand, you need to do that. 据我了解,您需要这样做。 Either that, or re-bind with the data on every postback. 或者,或者在每次回发时将其重新绑定到数据。

Here's what I like to do inside User Controls. 这是我喜欢在用户控件内执行的操作。 I apologize for this being C# and not VB, but I don't know VB: 我为C#而不是VB表示歉意,但我不知道VB:

    public string Text {
            get { return (string)ViewState["Text"]; }
            set { ViewState["Text"] = value; }
    }

Reference: https://weblogs.asp.net/infinitiesloop/Truly-Understanding-Viewstate 参考: https : //weblogs.asp.net/infinitiesloop/Truly-Understanding-Viewstate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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