维护一个中继器的视图状态 [英] Maintaining viewstate of a repeater

查看:206
本文介绍了维护一个中继器的视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,即一个中继器即中继器中的控件的视图状态没有维修器材自己的ViewState

I have a problem whereby the viewstate of a repeater i.e. the controls within the repeater are not maintaing their viewstate.

我有以下内容:

直放站1:

<asp:Repeater ID="rptImages" runat="server">
<ItemTemplate>
    <asp:LinkButton Text="Add" CommandName="Add" CommandArgument=<%# Eval("ID") %> runat="server" />
</ItemTemplate>
</asp:Repeater>

当链接按钮被点击时存储在页上的隐藏字段CommandArgument的值。

When the link button is clicked the value of the CommandArgument is stored in a hidden field on the page.

当回传我不能得到的隐藏字段的值,直到PreRender事件处理程序加载。所以在我的PreRender事件我抢隐藏字段的值,它存储在一个列表属性,像这样:

Upon postback I can't get the value of the hidden field until the prerender event handler has loaded. So in my prerender event I grab the value of the hidden field and store it in a List property, like so:

if (!string.IsNullOrEmpty(this.SelectedImageIDsInput.Text)) {
        this.ImageList.Add(this.SelectedImageIDsInput.Text);
    }

和List属性看起来像这样:

And the List property looks like so:

public List<string> ImageList {
    get {
        if (this.ViewState["ImageList"] == null) {
            this.ViewState["ImageList"] = new List<string>();
        }
        return (List<string>)(this.ViewState["ImageList"]);
    }
    set { this.ViewState["ImageString"] = value; }
}



一旦我存储的值进入我的列表属性我结合我的第二个中继器(再次在PreRender事件):

Once I have stored the value into my List property I bind my second repeater (again within the prerender event):

this.rptSelectedImages.DataSource = this.LightBoxControl.ImageList;
this.rptSelectedImages.DataBind();



第二个中继器具有一个DropDownList并在其中一个文本框。问题是,这些子控件的视图状态不能保持。我相信这是因为每次回发时,我重新绑定中继器,因此它被重建。我不知道我是怎么都不可能避开这个问题? ImageList属性只在回发更新,因此我显然要重新绑定与每次回发中继 - ?怎么回事能不能做到

The second repeater has a dropdownlist and a textbox within it. The problem is that the viewstate of these child controls is not maintained. I presume it is because with each postback I am rebinding the repeater, therefore it is rebuilt. What I don't know is how I can possibly get round this? The ImageList property is only updated upon a postback, so I obviously have to rebind the repeater with each postback - how else can it be done?

任何帮助将不胜感激

由于

推荐答案

如果你是重新绑定中继器,你需要做它初始化的ViewState之前被加载。

If you are rebinding the repeater, you need to do it on Init before the ViewState is loaded.

您也应该检查的IsPostBack 标记,只有绑定网页时不贴背面的中继器。

You should also check the IsPostback flag and only Bind the repeater when the page is not posted back.

要澄清,如果你的第二个中继器,势必在的PreRender 然后的ViewState 不能用坚持的控件,因为他们根本当的ViewState 加载不存在 - 初始化之后,和之前预紧力

To clarify if your second repeater is bound on PreRender then ViewState cannot be used to persist the controls, because they simply don't exist when ViewState is loaded - after Init, and before PreLoad.

您要么需要继续在每次回发,或存储或列表绑定在会话,让你有访问列表对初始化一旦绑定,(或改变)。

You either need to continue binding on every postback, or store or list in Session so that you have access to the list to bind once on Init, (or on change).

这篇关于维护一个中继器的视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆