为什么 ASP.NET ViewState 保留为 asp:dropdownlist 而不是 asp:table?

发布于 2024-10-11 07:29:44 字数 502 浏览 3 评论 0原文

好吧,对于你们中的许多人来说,这个问题可能有一个非常明显的答案,但它让我难住了。

我有一个 asp.net Web 表单,上面有两个控件(嗯,不止这两个,但我们将重点关注这些) - 第一个是 asp:dropdownlist,第二个是一个 asp:table

这两个控件都在 HTML 端声明,并在代码隐藏页面中填充(添加子控件)。

我的简单问题(希望有一个简单的答案)是这样的:

为什么视图状态持续存在于下拉列表中,而不是表中?

我必须在每次页面加载时填充表格,但我可以填充下拉列表一次(使用 Not Page.IsPostBack),并且它会持续存在。

注意:我已经了解了 ASP.NET 页面的生命周期,并且尝试在 Init() 和 PreInit() 页面事件中放置这些相同的调用,并获得相同的结果。

我在这里遗漏了哪些明显的细节?

感谢您的帮助。

Alright, there is probably a super-obvious answer to this question for many of you, but it has me stumped.

I have an asp.net web form, and on it I have two controls (well, more than these two but we'll focus on these) - the first is an asp:dropdownlist and the second is an asp:table.

Both of these controls are declared on the HTML side, and filled (child controls added) in the code-behind page.

My simple question (hopefully with a simple answer) is this:

Why does the viewstate persist for the dropdownlist, and NOT for the table?

I have to fill the table on every page load, but I can fill the dropdownlist once (using Not Page.IsPostBack), and it persists.

NOTE: I have read about the lifecycle of ASP.NET pages, and I have tried placing these same calls in the Init() and PreInit() page events with the same results.

What obvious detail am I missing here?

Thanks for the help.

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

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

发布评论

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

评论(3

夜访吸血鬼 2024-10-18 07:29:44

你没有遗漏任何东西,你的评估是正确的。 ASP.NET 表不会将其内容保存到视图状态。

我认为至少部分原因是表可以包含任意数量的任何类型的数据,并且在某些情况下可能真正开始增加视图状态的大小。

不管怎样,这就是他们的工作方式。如果您的页面上可以进行回发,则您需要在每个加载事件上重新填充表,或者存储表数据以自行查看状态,然后从数据重新填充表。

You're not missing anything, your assessment is correct. ASP.NET tables do not save their contents to view state.

I assumed at least part of the reason is that a table can contain any amount of any type of data and could really start to add to the size of the view state in some cases.

Either way, that's the way they work. If postbacks are possible on your page, you'll need to either repopulate the table on each load event, or store the table data to view state yourself and then repopulate the table from the data.

jJeQQOZ5 2024-10-18 07:29:44

asp:table 对象不会将其内容存储在 ViewState 中,因为它不是数据绑定控件。在这方面,它的工作方式与 asp:panel 相同;如果以编程方式向其中添加控件,则需要在每次回发时执行此操作,否则其中的项目将不会保留。

The asp:table object does not store its contents in ViewState, as it is not a data bound control. It works the same as an asp:panel in this regard; if you add controls to it programmatically, you need to do so on each post-back, or the items in it will not persist.

十级心震 2024-10-18 07:29:44

如果您想将表的内容存储在视图状态中 - 我不建议这样做,但有时您需要这样做......您只需使用标准的 asp.net 表控件即可。

<asp:Table ID="Table1" runat="server">
    <asp:TableRow ID="TableRow1" runat="server">
        <asp:TableCell ID="TableCell1" runat="server">
        This is Cell 1
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        This is Cell 2
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

您可以在 http://weblogs.asp.net/nannettethacker/archive/2008/01/24/html-tables-to-asp-net-table-controls.aspx

您还可以尝试将 runat="server" 添加到 table 标记。

没有真正直接回答您的问题,但认为这可能会有所帮助。

If you wanted to store the contents of the table in viewstate - which I woudln't recommend, but sometimes you need to do so ... you would just have to use the standard asp.net table control.

<asp:Table ID="Table1" runat="server">
    <asp:TableRow ID="TableRow1" runat="server">
        <asp:TableCell ID="TableCell1" runat="server">
        This is Cell 1
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        This is Cell 2
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

You can read more how to do this at http://weblogs.asp.net/nannettethacker/archive/2008/01/24/html-tables-to-asp-net-table-controls.aspx.

You might also try adding runat="server" to the table tag.

Not really answering your question directly, but thought it might be helpful nonetheless.

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