asp.net面板在回发时丢失viewstate [英] asp.net panel loses viewstate on postback

查看:78
本文介绍了asp.net面板在回发时丢失viewstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有更新面板的网页.在更新面板中,我有一个带有用户控件的面板,如下所示:

I have a web page with an update panel in it. Inside the update panel I have a panel with a user control like this:

<asp:UpdatePanel ID="updatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Panel ID="pnlFiles" runat="server" EnableViewState="true">
        <files:FilesControl runat="server" ID="filesControl" />
    </asp:Panel>
</ContentTemplate>

我有一个复选框列表,每当用户选中其中一个复选框时,我都想向该面板添加另一个 FilesControl 所以我这样做是这样的:

I have a list of checkboxes that whenever the user checks one of them I want to add another FilesControl to that panel so I am doing it like this:

FilesControl files = (FilesControl)LoadControl("~/UserControls/FilesControl.ascx");
files.ID = XXX;
pnlFiles.Controls.Add(files); 

但是在每次回发(选中每个复选框)时,面板都会丢失最后一个状态,并且删除的控件也将消失,因此,面板实际上每次都返回其初始状态,因此我无法向其添加越来越多的控件.我启用了视图状态,但无济于事.我在这里错过了什么?

But on every postback (every checkbox checked) the panel loses the last state and the controls added are wiped out, so the panel actually returns to its initial state every time and I can't ad more and more controls to it. I enabled view state and it didn't help. What am I missing out here ?

推荐答案

根据要求,我的评论将作为答案:

As requested, my comments as answer:

您需要将控件添加到 Page_Init Page_Load (最晚)中的每个回发页面上,并且具有与以前相同的ID.

You need to add the control(s) to the page on every postback in Page_Init or Page_Load(at the latest) with the same ID as before.

该ID是唯一的,我会生成它.我在复选框上添加控件已检查的更改事件chkCompare_CheckedChanged

The ID is unique, I generate it. I add the controls on the checkbox checked change event chkCompare_CheckedChanged

虽然在事件中动态添加控件非常好,但有必要在后续的回发中重新创建此控件.最晚必须在 Page_Load 中完成(最好是 Page_Init ).因此,您需要将已添加控件的位置存储在某个位置,以便能够重新创建它们.也许将控制计数存储在 ViewState 中即可.

While it is perfectly fine to add a control dynamically in an event, it is necessary to re-create this control on the subsequent postbacks. That must be done in Page_Load at the latest (better: Page_Init). So you need to store somewhere which controls you have already added to be able to re-create them. Maybe it is sufficient to store the control-count in the ViewState.

因此,我应该存储在ViewState对象中添加的控件,并然后将它们重新添加到Page_Load上的面板上?

So I should store the controls I added in the ViewState object and then re-add them to the panel on Page_Load ?

否,您不应将控件存储在 ViewState 中,而应将其重新创建所需的信息.您怎么知道您需要添加什么?当然,您也可以使用Webdatabound控件,例如 Repeater DataList GridView ,只需分配一个 DataSource 持久化的东西会自动为您完成.

No, you shouldn't store the controls in the ViewState but the information that is necessary to re-create them. How do you know what you need to add? Of course you could also use a webdatabound control like Repeater, DataList or GridView where you only need to assign a DataSource and the persisting stuff is done automatically for you.

即使您声明性地(在aspx上)添加所有控件,也可以这样做.但是这些控件是由ASP.NET自动重新创建的.所有变量都位于页面 life的末尾-cycle ,因为客户端与服务器之间的通信是无状态且已断开.

That is what is done with all controls even if you add them declaratively(on aspx). But those controls are re-created automatically by ASP.NET. All variables are disposed at the end of the page's life-cycle since the communication between the client and the server is stateless and disconnected.

这篇关于asp.net面板在回发时丢失viewstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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