C# asp.net dropdownlist和viewstate
我有一个下拉列表,它触发自动回发并触发SelectedIndexChanged事件。我已将viewstate设置为true,但由于某些原因,所选值在回发之间不会持久化。我已经使用下拉列表数百次了,但似乎无法解释为什么会发生这种情况。dropdownlist中的项目是声明性编码的,例如C# asp.net dropdownlist和viewstate,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我有一个下拉列表,它触发自动回发并触发SelectedIndexChanged事件。我已将viewstate设置为true,但由于某些原因,所选值在回发之间不会持久化。我已经使用下拉列表数百次了,但似乎无法解释为什么会发生这种情况。dropdownlist中的项目是声明性编码的,例如 <asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True" onselectedindexchanged="
<asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True"
onselectedindexchanged="SitePrefDropDownList_SelectedIndexChanged" EnableViewState="true">
<asp:ListItem Value="Proffesional">Proffesional</asp:ListItem>
<asp:ListItem Value="Colorful">Colorful</asp:ListItem>
</asp:DropDownList>
我能想到的唯一原因是,您正在页面加载事件中设置一些默认值。。像
protected void Page_Load(object sender, EventArgs e)
{
SitePrefDropDownList.SelectedValue = "Proffesional";
}
在页面生命周期中触发SitePrefDropDownList\u SelectedIndexChanged
事件之前,首先调用页面加载事件,并重置默认/旧值
编辑:页面加载应设置如下值:
if (!Page.IsPostback)
{
SitePrefDropDownList.SelectedValue = "Proffesional";
}
是否已在页面本身上禁用ViewState 即使控件本身已启用ViewState,如果页面已禁用ViewState,则不会记录页面上任何控件的ViewState-请参阅MSDN上的文档 检查页面的
EnableViewState
属性是否已在.aspx或代码隐藏中被禁用:
// Any statements that look like this could be the source of your woes
this.EnableViewState = false;
Page.EnableViewState = false;
<%@ Page EnableViewState="false" ...
//任何看起来像这样的陈述都可能是你痛苦的根源
this.EnableViewState=false;
Page.EnableViewState=false;
更改母版页会导致此类错误。
在会话中保存masterpagefilename,并在preinit方法的回发中设置它
protected override void OnPreInit(EventArgs e)
{
if (!IsPostBack)
{
this.MasterPageFile = "../../04.07.ManifestoKontrol.Web/ManifestoKontrolMasterPage.master";
Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)] = this.Master.AppRelativeVirtualPath;
base.OnPreInit(e);
}
else
{
if (Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)] != null)
this.MasterPageFile = Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)].ToString();
}
}
您看到值和文本没有错误吗
<asp:ListItem Text="20" Value="20" Selected="True" />
<asp:ListItem Text="50" Value="20" Selected="False" />
您在哪里检查所选值?我找不到。DDL触发selectedindexchanged
事件,但所选值不会在回发之间持久化。你能确认这是真的吗?这个DropDownList是否在GridView、FormView、UserControl等任何数据绑定容器中,并且在事件触发之前你是否调用它的DataBind
(例如,missingIsPostBack
-check)?你为什么要执行Server.Transfer(Request.Path)代码>?使用新选择的母版页重新加载页面。我在听教程不,我已经查过了。我还检查了dropdownlist本身的加载事件。如果这是正确答案,我建议添加解决方案:if(!page.IsPostback){}
@Richard Banks;你能告诉我你最后发现了什么吗?你在你那边测试过吗?我已经在页面级别禁用了viewstate,它对我来说仍然可以正常工作。EnableViewState在masterpage、contentpage和控件本身上为true。您可以在OnPreInit中编写此语句吗?MasterPageFile是OnPreInit,因为我遇到了此错误。只能在“Page_PreInit”事件中或之前设置“MasterPageFile”属性。
protected override void OnPreInit(EventArgs e)
{
if (!IsPostBack)
{
this.MasterPageFile = "../../04.07.ManifestoKontrol.Web/ManifestoKontrolMasterPage.master";
Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)] = this.Master.AppRelativeVirtualPath;
base.OnPreInit(e);
}
else
{
if (Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)] != null)
this.MasterPageFile = Session[String.Concat(DefaultMasterPageSessionVariableName, this.ClientID)].ToString();
}
}
<asp:ListItem Text="20" Value="20" Selected="True" />
<asp:ListItem Text="50" Value="20" Selected="False" />