用ASP.NET怎样动态给dropdownlist控件赋值

用ASP.NET怎样动态给dropdownlist控件赋值,第1张

1.绑定数据源

2.代码循环添加

for(int i=0i<100i++)

{

ListItem item=new ListItem(i.ToString(),"手动添加"+i.ToString())

dropdownList.Items.Add(item)

}

3.还可以手动添加

有两种方法:一种是用dual表查空值,然后绑定。另一种是先动态绑定数据后,然后插入一列默认项。

eg1:

string selectQuery = "select  null extattrid,null extattrname from dual union all select  extattrid,extattrname from VExtAttrDetail where extattrsn in (22)"

     DataSet ds=dboperReq.GetDSet(selectQuery)

    ddlTrantype.DataTextField = "extattrname"

    ddlTrantype.DataValueField="extattrid"

    ddlTrantype.DataSource=ds.Tables[0].DefaultView

    ddlTrantype.DataBind()

eg2:

 string selectQuery = "select  extattrid,extattrname from VExtAttrDetail where extattrsn in (22)"

    DataSet ds=dboperReq.GetDSet(selectQuery)

    ddlTrantype.DataTextField = "extattrname"

    ddlTrantype.DataValueField="extattrid"

    ddlTrantype.DataSource=ds.Tables[0].DefaultView

    ddlTrantype.DataBind()

    ddlTrantype.Items.Insert(0,new ListItem(" "," ")) //或者ListItem it = new ListItem("请选择", "0") 

//mydroplist.Items.Insert(0,it)     或者 ListItem lt = new ListItem() 

//lt.Text = "请选择" 

//lt.Value = "00" 

//lt.Selected = true 

//ddp.Items.Add(lt)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11365569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存