4

default.aspx 页面有一个DropDownList,只有当它不是postback. 选择值时,调用Literal具有所选值的方法填充a。它按预期工作。问题是当我将页面设置为仅在控件中不EnableViewState启用它时。DropDownList在这种情况下,当被退回时,DropDownList它会丢失它的物品。我已经设置了一个新的 Web 项目来测试它。没有母版页可以使其更简单。

默认.aspx:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" 
    EnableViewState="false" %>

<asp:DropDownList ID="DDL" runat="server" 
    OnSelectedIndexChanged="DDL_OSIC" 
    AutoPostBack="true" 
    EnableViewState="true">
</asp:DropDownList>

<asp:Literal ID="Literal1" runat="server"></asp:Literal>

默认.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DDL.Items.Add("red");
        DDL.Items.Add("green");
        DDL.Items.Add("blue");
    }
}
protected void DDL_OSIC(object sender, EventArgs e)
{
    Literal1.Text = DDL.SelectedValue;
}

为什么 EnableViewState 不起作用?

4

2 回答 2

4

在已删除的答案的帮助下找到了答案。删除的答案是错误的,因为它不完整。有了这个提示,我找到了 ViewStateMode 属性页

Summarizing it to disable all controls' ViewState and enable it just for the choosen ones:

  • Set both the page and all the control's EnableViewState property to true. This is the default so it is not necessary to write anything
  • Set the page ViewStateMode to Disabled
  • Set ViewStateMode to Enabled in the control where you want ViewState enabled
于 2012-08-02T23:17:47.223 回答
-3

In my computer,I create a web application.The result is same with you. I guess,when the page viewstate is false,control viewstate can't work.

于 2012-09-13T09:56:31.873 回答