日期:2014-05-18  浏览次数:20498 次

dropdownlist绑定数据后无法显示
dropdownlist2是通过配置数据源得到的 代码如下:public partial class _Default : System.Web.UI.Page 
{
  string curPage;
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  this.lblPageCur.Text = "1";//不能放到dataGridBind()后面,不然lblPageCur.Text没有被初始化,出错
  dataGridBind();  
  }
  }
  public void dataGridBind()
  {
  string s = DropDownList2.SelectedValue.ToString(); //如果去掉dropdownlist2的话,cmd.amnadText="select * from guest"可以绑定数据  
  curPage =this.lblPageCur.Text;
  SqlConnection conn = DB.createCon();
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = string .Format("select * from guest where ower='{0}' order by PostTime desc",s);
  cmd.Connection = conn;
  SqlDataAdapter sda = new SqlDataAdapter();
  sda.SelectCommand = cmd;
  DataSet ds = new DataSet();
  sda.Fill(ds, "guest");
  PagedDataSource pds = new PagedDataSource();
  pds.AllowPaging = true;
  pds.PageSize = 3;
  pds.DataSource = ds.Tables["guest"].DefaultView;
  pds.CurrentPageIndex = Convert.ToInt32(curPage) - 1;
  this.lblPageTotal.Text = pds.PageCount.ToString();
  this.Button1.Enabled=true;
  this.Button2.Enabled=true;
  if (curPage == "1")
  {
  this.Button1.Enabled = false;
  }  
  if (curPage == pds.PageCount.ToString())
  {
  this.Button2.Enabled = false;
  }
  this.DataList1.DataSource = pds;
  this.DataList1.DataBind();

  cmd.CommandText = "select count(*) from guest";
  this.lblMesTotal.Text = Convert.ToString(cmd.ExecuteScalar());

  int a = pds.PageCount;
  for(int i=1;i<=a;i++)
  {
  this.DropDownList1.Items.Add(i.ToString());
  }
  }

------解决方案--------------------
this.DataList1.DataSource = pds; 
//加上
this.DataList1.DataTextField="guestName";//仅是个说明,这里是在显示出的字段
this.DataList1.DatValueField="guestId";//仅是个说明,这里是值的字段
this.DataList1.DataBind();
------解决方案--------------------
asp.net夜话之八:数据绑定控件
在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。
------解决方案--------------------
注意,第二个DropDownList控件绑定数据源时有两句话必不可少,就是:
C# code
ddlUserList.DataTextField = "RealName";//指定下拉列表中的文字显示部分 
ddlUserList.DataValueField = "UserID";//指定下拉列表中的值部分

------解决方案--------------------
数据绑定就是这几步
写好后应该没有什么问题
DropDownList2.DataTextField = "Text值";//指定下拉列表中的文字显示部分 
DropDownList2.DataValueField = "value值";//指定下拉列表中的值部分

dropDownList2.DataSource=数据源;
dropDownList2.DataBind();

---