开发者

Why is ASP.NET ViewState retained with an asp:dropdownlist but not an asp:table?

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 views开发者_StackOverflow社区tate 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜