Asp.net下拉列表和视图状态

本文关键字:视图状态 下拉列表 net Asp | 更新日期: 2023-09-27 18:01:52

我有一个触发autopostback并触发SelectedIndexChanged更改事件的下拉列表。我将viewstate设置为true,但由于某些原因,所选值在回发之间没有被持久化。我使用下拉列表数百次,但似乎不知道为什么会发生这种情况。下拉列表中的项是声明性编码的,例如

<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> 

任何想法。我难住了

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Proffesional.master" AutoEventWireup="true" EnableViewState="true"
CodeFile="Home.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="Label1" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Color Scheme: ">
</asp:Label><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>
<br />
<br />
<asp:Button ID="OKButton" runat="server" Text="OK" onclick="OKButton_Click" />
</asp:Content>

下面是

后面的代码
 public partial class _Default : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e) {
    if (Session["Template"] != null) {
        string MasterPage = String.Format("~/{0}.master", (string)Session["Template"]);
        MasterPageFile = MasterPage;
    }
}
protected void Page_Load(object sender, EventArgs e){
}
protected void OKButton_Click(object sender, EventArgs e) {
    if (UserNameTextBox.Text.Length != 0) {
        Session["UserName"] = UserNameTextBox.Text;
        Label Welcome = (Label)Master.FindControl("GreetingLabel");
        Welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
    }
}
protected void SitePrefDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
    Session["Template"] = SitePrefDropDownList.SelectedValue;
    Server.Transfer(Request.Path);
}
}

Asp.net下拉列表和视图状态

我能想到的唯一原因是你在页面加载事件中设置了一些默认值。像. .

protected void Page_Load(object sender, EventArgs e)
{
    SitePrefDropDownList.SelectedValue = "Proffesional";
}

在页面生命周期中SitePrefDropDownList_SelectedIndexChanged事件触发之前,首先调用Page_load事件,然后重置您的Default/Old值

编辑:你的页面加载值应该设置为…

if (!Page.IsPostback)
{
   SitePrefDropDownList.SelectedValue = "Proffesional";
}

是否禁用了页面本身的ViewState ?

即使控件本身启用了ViewState,如果页面禁用了ViewState,那么页面上的任何控件都不会记录ViewState -参见页面上的文档。MSDN上的EnableViewState属性。

检查页面的EnableViewState属性是否被禁用,无论是在.aspx中还是在后面的代码中:

// Any statements that look like this could be the source of your woes
this.EnableViewState = false;
Page.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" />