asp.net的DropDownList和视图状态 [英] asp.net dropdownlist and viewstate

查看:112
本文介绍了asp.net的DropDownList和视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有触发自动回发,并触发的SelectedIndexChanged事件改变一个DropDownList。香港专业教育学院的视图状态设置为true,但由于某种原因选择的值不回发之间持久化。香港专业教育学院使用的DropDownList的几百次,但不能似乎找出为什么发生这种情况。在DropDownList的项目是声明codeD例如

 < ASP:DropDownList的ID =网站prefDropDownList=服务器的AutoPostBack =真
onselectedindexchanged =网站prefDropDownList_SelectedIndexChanged的EnableViewState =真>
    < ASP:ListItem的值=业余体校>及业余体校LT; / ASP:ListItem的>
    < ASP:ListItem的值=七彩>七彩< / ASP:ListItem的>
< / ASP:DropDownList的>

任何想法。林难倒

 <%@页标题=主页LANGUAGE =C#的MasterPageFile =〜/ Proffesional.masterAutoEventWireup =真的EnableViewState =真
codeFILE =Home.aspx.cs继承=_默认%GT;< ASP:内容ID =HeaderContent=服务器ContentPlaceHolderID =HeadContent>
< / ASP:内容>
< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
< ASP:标签ID =Label1的=服务器文本=用户名:>< / ASP:标签>
< ASP:文本框ID =UserNameTextBox=服务器>< / ASP:文本框>
< BR />
< BR />
< ASP:标签ID =Label2的=服务器文本=配色方案:>
< / ASP:标签>< ASP:DropDownList的ID =网站prefDropDownList=服务器
的AutoPostBack =真
onselectedindexchanged =网站prefDropDownList_SelectedIndexChanged的EnableViewState =真>
    < ASP:ListItem的值=业余体校>及业余体校LT; / ASP:ListItem的>
    < ASP:ListItem的值=七彩>七彩< / ASP:ListItem的>
< / ASP:DropDownList的>
< BR />
< BR />
< ASP:按钮的ID =OKButton=服务器文本=OK的onclick =OKButton_Click/>
< / ASP:内容>

继承人的$ C $后面

ç

 公共部分类_Default:System.Web.UI.Page
{
保护无效Page_ preINIT(对象发件人,EventArgs的发送){
    如果(会话[模板]!= NULL){
        字符串母版=的String.Format(〜/ {0}的.master(字符串)会议[模板]);
        的MasterPageFile =母版;
    }
}保护无效的Page_Load(对象发件人,EventArgs的发送){
}保护无效OKButton_Click(对象发件人,EventArgs的发送){
    如果(UserNameTextBox.Text.Length!= 0){
        会话[用户名] = UserNameTextBox.Text;
        标签欢迎=(标签)Master.FindControl(GreetingLabel);
        Welcome.Text =的String.Format(欢迎{0}!,会话[用户名]);
    }
}保护无效站点prefDropDownList_SelectedIndexChanged(对象发件人,EventArgs的发送){
    会话[模板] =网站prefDropDownList.SelectedValue;
    Server.Transfer的(Request的);
}
}


解决方案

我能想到的唯一原因,是您在设置页面的加载事件的一些默认值..喜欢..

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    网站prefDropDownList.SelectedValue =业余体校;
}

在页面生命周期中的网​​站prefDropDownList_SelectedIndexChanged 事件触发之前,Page_Load事件被称为第一次和你的默认/旧值将被重置

编辑:您的页面加载应该设置如..值

 如果(!Page.IsPostback)
{
   网站prefDropDownList.SelectedValue =业余体校;
}

I have a dropdownlist that triggers autopostback and fires the SelectedIndexChanged change event. Ive set viewstate to true but for some reason the selected value is not persisted between postbacks. Ive used the dropdownlist hundreds of times but cant seem to work out why this is happening. The items in the dropdownlist are declaratively coded e.g

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

Any ideas. Im stumped

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

heres the code behind

 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);
}
}

解决方案

The only reason I can think of, is you are setting some default value in your Page load event.. like..

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

Before the SitePrefDropDownList_SelectedIndexChanged event fires in the page life cycle, the Page_load event is called first and your Default/Old value will be reset

Edit: Your page load should set the value like..

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

这篇关于asp.net的DropDownList和视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆