读书人

asp.net DropDownList回发丢值!解决方

发布时间: 2012-04-24 14:15:38 作者: rapoo

asp.net DropDownList回发丢值!
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" EnableViewState="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>



string iid = DropDownList1.SelectedValue;

回发获取不到值,是空的。



绑定的地方:

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

Dal.sqlSelsct slt = new Dal.sqlSelsct();
DataSet dss = slt.selectSql("select * from union_adtype where type_lever=1");
this.DropDownList1.DataSource = dss.Tables[0];
this.DropDownList1.DataTextField = "type_name";
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataBind();

}

}



我把DropDownList1是放在!IsPostBack里面,但是每次回发:

string iid = DropDownList1.SelectedValue;取到的都是空,我设断点发现DropDownList1里面回发之后没有了item。所以我怀疑是EnableViewState有问题,没有把状态保存下来回发。求高手指点。

<form id="form1" runat="server">

表单是runat="server"

而且有一点,textbox是能正常回发值,就是这个DropDownList1回发就丢了值。


[解决办法]
代码放到 if (!IsPostBack) 外面来

读书人网 >asp.net

热点推荐