的EnableViewState不DropDownList的工作 [英] EnableViewState not working in DropDownList
问题描述
Default.aspx页面有一个的DropDownList
这是在人口仅次于code,当它不是一个回发
。当选择一个值的方法被称为填补一个文字
与所选值。它按预期工作。问题是,当我不的EnableViewState
使其在的DropDownList
控制只设置页面。在这种情况下,当回发的的DropDownList
失去了它的项目。我已经安装一个新的Web项目只是为了测试这一点。有没有高手页面,使其更简单。
The default.aspx page has a DropDownList
which is populated in the code behind only when it is not a postback
. When a value is selected a method is called which fills a Literal
with the selected value. It works as expected. The problem is when I set the page to not EnableViewState
enabling it in the DropDownList
control only. In this case when posted back the DropDownList
loses its items. I have setup a new Web project just to test this. There is no master page to make it simpler.
Default.aspx的:
Default.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>
Default.aspx.cs:
Default.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?
Why isn't EnableViewState working?
推荐答案
找到了已删除的答案的帮助答案。被删除的答案是错的,只是因为它是不完整的。与暗示我发现的ViewStateMode属性页一>
Found the answer with the help of a deleted answer. The deleted answer was wrong just because it was incomplete. With that hint I found the ViewStateMode Property page.
总结它禁用所有控件的ViewState中并启用它只是用于已选定的:
Summarizing it to disable all controls' ViewState and enable it just for the choosen ones:
- 设置两个页面,所有控件的
的EnableViewState
属性真正
。这是默认的,因此没有必要写什么 - 设置页面
ViewStateMode
到禁用
- 设置
ViewStateMode
到在你想要启用ViewState的控制已启用
- Set both the page and all the control's
EnableViewState
property totrue
. This is the default so it is not necessary to write anything - Set the page
ViewStateMode
toDisabled
- Set
ViewStateMode
toEnabled
in the control where you want ViewState enabled
这篇关于的EnableViewState不DropDownList的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!