dropdownlist绑定和选中
最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家:
现在是后台数据绑定
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void BindCarID() 2 { 3 DataTable dt = new DataTable(); 4 //这里是数据库查询出来的(sql语句和数据库连接,自己写) 5 string sql="select * from table1"; 6 if (dt != null && dt.Rows.Count > 0) 7 { 8 dt.AsEnumerable().ToList().ForEach( 9 (i) => 10 { 11 if (i["Type"].ToString().Trim() == "1") 12 { 13 ddlbank.Items.Add(new ListItem(i["Name"].ToString().Trim(), i["ID"].ToString().Trim())); 14 } 15 else 16 { 17 ddlbank.Items.Add(new ListItem(i["Name"].ToString().Trim() + "(" + i["CardName"].ToString().Trim() + ")", i["ID"].ToString().Trim())); 18 } 19 } 20 21 ); 22 /*ddlbank.DataSource = dt; 23 ddlbank.DataValueField = "ID"; 24 ddlbank.DataTextField = "Name"; 25 ddlbank.DataBind();*/ 这里是直接数据源绑定 26 } 27 ddlbank.Items.Insert(0, new ListItem("现金", "0")); 28 //新增一个下拉着 29 } 30
清空dropdownlist列值的方法:
dropdownlist1.Items.Clear(); //情况看所有数据
dropdownlist1.Items.Remove("bbb"); //清空一行数据
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("中国"));
就是如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1
设置dropdownlist1选中值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 hidfg.Value="文本";//服务器隐藏控件 2 3 for (int i = 0; i < dropdownlist1.Items.Count; i++) 4 { 5 if (hidfg.Value.Equals(dropdownlist1.Items[i].Value)) 6 { 7 dropdownlist1.Items[i].Selected = true; 8 9 } 10 }
接下来我们进行js+ashx进行数据显示
function GetBanks(id, name) { $("#Allbank").empty();//清空dropdown $.ajax({ type: "POST", url: 'AjaxDf/BkAjax.ashx?OPT=banks', //处理页的相对地址 data: { guestid: id, opts: name }, //参数类型 success: function (dates) { if (dates != "") { var t = $.parseJSON(dates); var f = t.rows; $('#Allbank').append("<option value='-2' Selected='True'>其它</option>"); if (f.length > 0) { //绑定dropdown for (var i = 0; i < f.length; i++) { $('#Allbank').append("<option value=" + f[i].BankID + ">" + f[i].BankName + "</option>"); } } $('#yh').css("display", ""); $('#Allbank').css("display", ""); } } }); }
ashx页面获取的数据,在返回给页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 获取某个人的银行信息 2 public string GetBank(HttpContext context, int it) 3 { 4 classDBOP dbop = new classDBOP(); 5 DataTable dt = new DataTable(); 6 string guestid = ""; 7 if (context.Request.Params["guestid"] != null) 8 { 9 guestid = context.Request.Params["guestid"].ToString().Trim(); 10 } 11 string bid=""; 12 if (context.Request.Params["bid"] != null) 13 { 14 bid = context.Request.Params["bid"].ToString().Trim(); 15 } 16 string tps = ""; 17 string strwhere = ""; 18 if (it == 1) 19 { 20 strwhere = "BankID=" + bid + ""; 21 } 22 else 23 { 24 if (context.Request.Params["opts"] != null) 25 { 26 tps = context.Request.Params["opts"].ToString().Trim(); 27 28 } 29 strwhere = "TP='" + tps + "' and TPid=" + guestid; 30 } 31 dt = dbop.SchDataDT("table", "ID,Name,[Type]", strwhere, 4);//获取数据 32 int Counts = 0; 33 string bankNo = ""; 34 if (dt != null) 35 { 36 Counts = dt.Rows.Count; 37 for (int i = 0; i < dt.Rows.Count; i++) 38 { 39 bankNo = dt.Rows[0]["ID"].ToString(); 40 } 41 } 42 if (it == 1) 43 { 44 return bankNo; 45 } 46 else 47 { 48 string fh = jsonop.ToJsonString(dt, Counts); 49 return fh; 50 } 51 } 52 #endregion
以上是两种绑定方法,数据库连接和查询需要朋友们自己写哦。-