有两种方法:一种是用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);
红色是关键代码,那么如何选择请选择呢?也是两种方式,我比较倾向于后一种方式:
Default.aspx1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 namespace Wolfy.DropDownListDemo 9 { 10 public partial class Default : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 DropDownListProvince.DataSource = GetList(); 15 DropDownListProvince.DataTextField = "Name"; 16 DropDownListProvince.DataValueField = "ID"; 17 DropDownListProvince.DataBind(); 18 RepeaterList.DataSource = GetList(); 19 RepeaterList.DataBind(); 20 } 21 protected List<Province> GetList() 22 { 23 List<Province> list = new List<Province>(); 24 list.Add(new Province() { ID = 1, Name = "北京", Pid = 0 }); 25 list.Add(new Province() { ID = 2, Name = "上海", Pid = 0 }); 26 list.Add(new Province() { ID = 3, Name = "河南", Pid = 0 }); 27 list.Add(new Province() { ID = 4, Name = "河北", Pid = 0 }); 28 list.Add(new Province() { ID = 5, Name = "湖南", Pid = 0 }); 29 list.Add(new Province() { ID = 6, Name = "湖北", Pid = 0 }); 30 return list; 31 } 32 33 protected void LinkUpdate_Click(object sender, EventArgs e) 34 { 35 LinkButton link = sender as LinkButton; 36 if (link != null) 37 { 38 int id = Convert.ToInt32(link.CommandArgument); 39 Province pro = GetList().Find(a => a.ID == id); 40 this.LiteralID.Text = pro.ID.ToString(); 41 this.LiteralLevel.Text = pro.Pid.ToString(); 42 //DropDownList的index是从零开始的 而绑定的数据的ID是从1开始的,所以为了对应需减一 43 //改变默认选中项 44 this.DropDownListProvince.SelectedIndex = pro.ID-1; 45 } 46 } 47 } 48 }
方式二
1 protected void LinkUpdate_Click(object sender, EventArgs e) 2 { 3 LinkButton link = sender as LinkButton; 4 if (link != null) 5 { 6 int id = Convert.ToInt32(link.CommandArgument); 7 Province pro = GetList().Find(a => a.ID == id); 8 this.LiteralID.Text = pro.ID.ToString(); 9 this.LiteralLevel.Text = pro.Pid.ToString(); 10 //方式二 11 ListItem item = DropDownListProvince.Items.FindByText(pro.Name); 12 if (item != null) 13 { 14 //防止出现多选的情况,将选中项 清除 15 DropDownListProvince.ClearSelection(); 16 item.Selected = true; 17 } 18 } 19 }