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

一个关于DropDownList动态绑定数据源的问题!很简单,可是只是不可以,为什么......?
小弟在页面上先放了一个SqlDataSource控件,然后对其进行连接设置以及数据库表等一些设置(按向导一步步到最后的完成)。
现在小弟在.cs文件中写了如下程序:
      DropDownList   APPSamp   =   new   DropDownList();
      SqlDataSource.SelectCommandType   =SqlDataSourceCommandType.Text;
      SqlDataSource.SelectCommand   =   "SELECT   DISTINCT   ID,   name   FROM   TerminalCFMethods   WHERE   curuser   =   ' "   +   Username   +   " '   OR   property   =   1 ";
      SqlDataSource_Sample.Select(new   DataSourceSelectArguments());
      APPSamp.DataSourceID   =   "SqlDataSource_Sample ";
      APPSamp.DataTextField   =   "name ";
      APPSamp.DataValueField   =   "ID ";
      APPSamp.DataBind();
当程序运行到APPSamp.DataBind();这总是提示什么DropDownList容器为空,我晕,我不是给他赋值了吗?请各位帮帮忙!


------解决方案--------------------
try

DropDownList APPSamp = new DropDownList();
SqlDataSource.SelectCommandType =SqlDataSourceCommandType.Text;

> > >

DropDownList APPSamp = new DropDownList();
Page.Controls.Add(APPSamp); // here
SqlDataSource.SelectCommandType =SqlDataSourceCommandType.Text;