如何处理复合自定义控件中放置在可绑定模板上的子控件的视图状态?

发布于 2024-07-15 12:02:15 字数 314 浏览 5 评论 0原文

我有一个复合数据绑定控件,它托管 IBindableTemplate 并根据某些条件将标记动态加载到控件中。 现在,当这些子控件加载到复合控件中并且存在回发时,我会丢失子控件的视图状态。 有没有办法,我可以在回发时保存子控件的视图状态?

我也参考。 斯科特的解释使用 http://scottonwriting.net/sowblog/posts/2129.aspx; 但没有用。

I've a composite data-bound control which hosts a IBindableTemplate and dynamically loads a mark-up based on some condition into the control. Now, when these child controls are loaded into the composite control and postback is there, I lose viewstate of the child controls. Is there a way, I can save viewstate of the child-controls on the postback?

I also ref. to the Scott's explanation using http://scottonwriting.net/sowblog/posts/2129.aspx; but of no use.

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

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

发布评论

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

评论(2

心作怪 2024-07-22 12:02:15

没有足够的信息。 什么时候创建控件? 什么时候将它们添加到 Controls 集合中? 什么是条件?它会在回发时发生变化吗?

如果在正确的时间添加了控件,则视图状态会在页面周期结束时(回发或不回发)自动保存。

如果您稍后添加控件,在某些情况下是在完成所有初始化之后,那么就为时已晚。

更新

如果没有代码,就很难猜测故障发生在哪里。 让我们检查一个带有自定义模板的中继器,它可以根据某些条件加载控件。 此示例可以正常工作,但如果在 Page_Load 上完成模板分配,则会失败。 这是否与您的情况类似?

形式:

<div>
    <asp:Repeater ID="repeater" runat="server" />
    <asp:Button ID="submitButton" runat="server" Text="Submit" onclick="submitButton_Click" />
    <asp:Button ID="postButton" runat="server" Text="PostBack" />
</div>

代码:

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        repeater.ItemTemplate = new MyTemplate();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //however, if I was to move repeater.ItemTemplate = new MyTemplate() here
        //it would not reload the view state
        if (!IsPostBack)
        {
            repeater.DataSource = new int[] { 1, 2, 3, 4, 5 };
            repeater.DataBind();
        }
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        submitButton.Text = "Do it again";
    }
}

public class MyTemplate : IBindableTemplate, INamingContainer
{
    #region IBindableTemplate Members
    public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
    {
        OrderedDictionary dictionary = new OrderedDictionary();
        return dictionary;
    }
    #endregion

    #region ITemplate Members
    public void InstantiateIn(Control container)
    {
        Label label = new Label();
        label.Text = "Label";
        container.Controls.Add(label);

        TextBox textbox = new TextBox();
        container.Controls.Add(textbox);
    }
    #endregion
}

There is not enough information. When do you create controls? When do you add them to the Controls collection? What is condition and does it change on postback?

The viewstate is saved automatically at the end of the page cycle (postback or not) given that controls are added at the right time.

If you are adding controls later on, in some event after all initialization has been done, then it is too late.

Update

Without code it is difficult to guess where the break down occurs. Let's examine a Repeater with custom template which could load controls base on some condition. This sample is working, but it would fail if the template assignment was done on Page_Load. Is this something similar to your situation?

Form:

<div>
    <asp:Repeater ID="repeater" runat="server" />
    <asp:Button ID="submitButton" runat="server" Text="Submit" onclick="submitButton_Click" />
    <asp:Button ID="postButton" runat="server" Text="PostBack" />
</div>

Code:

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        repeater.ItemTemplate = new MyTemplate();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //however, if I was to move repeater.ItemTemplate = new MyTemplate() here
        //it would not reload the view state
        if (!IsPostBack)
        {
            repeater.DataSource = new int[] { 1, 2, 3, 4, 5 };
            repeater.DataBind();
        }
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        submitButton.Text = "Do it again";
    }
}

public class MyTemplate : IBindableTemplate, INamingContainer
{
    #region IBindableTemplate Members
    public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
    {
        OrderedDictionary dictionary = new OrderedDictionary();
        return dictionary;
    }
    #endregion

    #region ITemplate Members
    public void InstantiateIn(Control container)
    {
        Label label = new Label();
        label.Text = "Label";
        container.Controls.Add(label);

        TextBox textbox = new TextBox();
        container.Controls.Add(textbox);
    }
    #endregion
}
红尘作伴 2024-07-22 12:02:15

您的理论代码几乎接近我的情况,只有一个主要区别是我没有从 IBindable 扩展我的模板类,而是我在标记本身中提供模板的控件,这是要求。

Scott 还明确表示,如果我们像您一样将控件添加到模板中,

         #region ITemplate Members
         public void InstantiateIn(Control container)
         {
             Label label = new Label();
             label.Text = "Label";
             container.Controls.Add(label);

             TextBox textbox = new TextBox();
             container.Controls.Add(textbox);
         }
         #endregion

那么视图状态将在每次回发时自动保留。 我所需要的只是允许我的模板在标记中实例化,并且在回发时仍然保留视图状态,并允许我获取控件的状态,即使我曾经使用一些不同的数据重新启动模板。

Your theoretical code is almost near to my situation with only one major difference that I don't extend my template class from IBindable as such rather I provide the controls for the templates in the mark-up itself which is the requirement.

Scott also, makes it clear that if we add the controls to template like you have done

         #region ITemplate Members
         public void InstantiateIn(Control container)
         {
             Label label = new Label();
             label.Text = "Label";
             container.Controls.Add(label);

             TextBox textbox = new TextBox();
             container.Controls.Add(textbox);
         }
         #endregion

then the view-state will automatically be persisted on every postback. All I need is to allow my templates be instantiated in the mark-up and still persist viewstate on postback and allow me to fetch the status of control even if I used to re-initiate the template with some different data.

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