首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

dropDownList绑定数据有关问题

2012-02-06 
dropDownList绑定数据问题我想在此控件上加一个 ---请选择--- 我的绑定代码是这样写的:SqlConnectioncon

dropDownList绑定数据问题
我想在此控件上加一个 "---请选择--- "
我的绑定代码是这样写的:
SqlConnection   con   =   new   SqlConnection( "server=.;database=jqwdb;uid=sa ");
                string   sql   =   "select   *   from   goodsType ";
                SqlDataAdapter   da   =   new   SqlDataAdapter(sql,   con);
                DataSet   ds   =   new   DataSet();
                da.Fill(ds);
                this.DropDownList1.DataSource   =   ds.Tables[0].DefaultView;
                this.DropDownList1.DataTextField   =   "goodsType ";
                this.DropDownList1.DataValueField   =   "goodsId ";
                this.DropDownList1.DataBind();

[解决办法]
this.DropDownList1.DataBind();
加上一行

this.DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "top "));
[解决办法]
同上
[解决办法]
加上
this.DropDownList1.Items.add(new ListItem( "---请选择--- ", "0 "));
this.DropDownList1.SelectedIndex=this.DropDownList1.Items.Count-1;

[解决办法]
lv = new ListItem();
lv.Text = "---请选择--- ";
lv.Value = "0 ";
DropDownList1.Items.Add(lv);
if (DropDownList1.Items.Count > 0)
{
DropDownList1or.SelectedIndex = 0;
}
[解决办法]
SqlConnection con = new SqlConnection( "server=.;database=jqwdb;uid=sa ");
string sql = "select * from goodsType ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "goodsType ";
this.DropDownList1.DataValueField = "goodsId ";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
[解决办法]
啊哦
[解决办法]
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
[解决办法]
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
[解决办法]
如果是VS2005,直接点智能标记,编辑项
[解决办法]
vs2005智能标记,编辑项 AppendDataBoundItems= "True "
[解决办法]
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
加“-1”是什么意思?
----------------
指定这一项的Value,根据实际情况,此值不要与其它绑定项的Value值相同


[解决办法]
Value
[解决办法]
指定这一项的Value,根据实际情况,此值不要与其它绑定项的Value值相同
-------------------------------------------------
up

楼主应该知道,Value写0,-1,1000,什么都行,供你日后判断取值之用
[解决办法]
楼上的正解,也可以在得到数据源后把“请选择”加入到数据源中,然后再绑定DropDownList控件
DataTable dt=ds.Tables[0];
DataRow therow=dt.NewRow();
therow[ "goodsType "]= "--请选择-- ";
therow[ "goodsID "]= "-1 ";
dt.Rows.Insert(therow,0);
this.DropDownList1.DataSource = dt.DefaultView;
this.DropDownList1.DataTextField = "goodsType ";
this.DropDownList1.DataValueField = "goodsId ";
this.DropDownList1.DataBind();
相对来说比较麻烦,不过也是一种办法.

热点排行