100分求助:dropdownlist数据绑定后选择下拉列表后返回的值不变或为空
前台代码:
<asp:DropDownList ID="Fanwei" runat="server" AutoPostBack="true"> </asp:DropDownList>
if (!IsPostBack) { SqlDataReader dr = this.Executeprocedure1("SelectZhuceCompanyType"); if (dr.Read()) { Fanwei.Items.Add(new ListItem(dr["ProductCatalogName_nvarchar"].ToString(), dr["ProductCatalogId_nvarchar"].ToString())); } DataSet ss = new DataSet(); this.Executeprocedure("SelectZhuceCompanyType", ref ss); Fanwei.DataSource = ss.Tables[0].DefaultView; Fanwei.DataTextField = "ProductCatalogName_nvarchar"; Fanwei.DataValueField = "ProductCatalogId_nvarchar"; Fanwei.Items.Clear(); Fanwei.DataBind(); ss.Clear(); ss.Dispose(); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindxx(); } } public void bindxx() { DataSet ds = gc.select_gyss(selecttype); GridView1.DataSource = ds; GridView1.DataBind(); ds.Clear(); ds.Dispose(); }
[解决办法]
DataSet ss = new DataSet();
this.Executeprocedure("SelectZhuceCompanyType", ref ss);
Fanwei.DataSource = ss.Tables[0].DefaultView;
Fanwei.DataTextField = "ProductCatalogName_nvarchar";
Fanwei.DataValueField = "ProductCatalogId_nvarchar";
Fanwei.DataBind();
SqlDataReader dr = this.Executeprocedure1("SelectZhuceCompanyType");
if (dr.Read())
{
Fanwei.Items.Insert(0,new ListItem(dr["ProductCatalogName_nvarchar"].ToString(), dr["ProductCatalogId_nvarchar"].ToString()));
}
单步跟踪看看Fanwei.SelectedValue是否有值
[解决办法]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Table.aspx.cs" Inherits="Table" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" DataSourceID="SqlDataSource1" DataTextField="Opinion" DataValueField="VoteID"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db_11ConnectionString %>" SelectCommand="SELECT [VoteID], [Opinion] FROM [Vote]"></asp:SqlDataSource> </div> </form></body></html>