最近在使用dropdownlist控件,对于这个控件,目前我知道的会使用两种方式去绑定数据,现在将这两种方式分享给大家:

现在是后台数据绑定

   protected void BindCarID()
{
DataTable dt = new DataTable();
//这里是数据库查询出来的(sql语句和数据库连接,自己写)
string sql="select * from table1";
if (dt != null && dt.Rows.Count > )
{
dt.AsEnumerable().ToList().ForEach(
(i) =>
{
if (i["Type"].ToString().Trim() == "")
{
ddlbank.Items.Add(new ListItem(i["Name"].ToString().Trim(), i["ID"].ToString().Trim()));
}
else
{
ddlbank.Items.Add(new ListItem(i["Name"].ToString().Trim() + "(" + i["CardName"].ToString().Trim() + ")", i["ID"].ToString().Trim()));
}
} );
/*ddlbank.DataSource = dt;
ddlbank.DataValueField = "ID";
ddlbank.DataTextField = "Name";
ddlbank.DataBind();*/ 这里是直接数据源绑定
}
ddlbank.Items.Insert(, new ListItem("现金", ""));
//新增一个下拉着
}

清空dropdownlist列值的方法:

dropdownlist1.Items.Clear(); //情况看所有数据

dropdownlist1.Items.Remove("bbb");          //清空一行数据

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("中国"));
就是如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1

设置dropdownlist1选中值

 hidfg.Value="文本";//服务器隐藏控件

 for (int i = ; i < dropdownlist1.Items.Count; i++)
{
if (hidfg.Value.Equals(dropdownlist1.Items[i].Value))
{
dropdownlist1.Items[i].Selected = true; }
}

接下来我们进行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 > ) {
//绑定dropdown
for (var i = ; i < f.length; i++) {
$('#Allbank').append("<option value=" + f[i].BankID + ">" + f[i].BankName + "</option>");
}
}
$('#yh').css("display", "");
$('#Allbank').css("display", ""); }
}
});
}

ashx页面获取的数据,在返回给页面

      #region  获取某个人的银行信息
public string GetBank(HttpContext context, int it)
{
classDBOP dbop = new classDBOP();
DataTable dt = new DataTable();
string guestid = "";
if (context.Request.Params["guestid"] != null)
{
guestid = context.Request.Params["guestid"].ToString().Trim();
}
string bid="";
if (context.Request.Params["bid"] != null)
{
bid = context.Request.Params["bid"].ToString().Trim();
}
string tps = "";
string strwhere = "";
if (it == )
{
strwhere = "BankID=" + bid + "";
}
else
{
if (context.Request.Params["opts"] != null)
{
tps = context.Request.Params["opts"].ToString().Trim(); }
strwhere = "TP='" + tps + "' and TPid=" + guestid;
}
dt = dbop.SchDataDT("table", "ID,Name,[Type]", strwhere, );//获取数据
int Counts = ;
string bankNo = "";
if (dt != null)
{
Counts = dt.Rows.Count;
for (int i = ; i < dt.Rows.Count; i++)
{
bankNo = dt.Rows[]["ID"].ToString();
}
}
if (it == )
{
return bankNo;
}
else
{
string fh = jsonop.ToJsonString(dt, Counts);
return fh;
}
}
#endregion

以上是两种绑定方法,数据库连接和查询需要朋友们自己写哦。-

dropdownlist绑定和选中的更多相关文章

  1. 下拉列表框DropDownList绑定Dictionary泛型类

    DropDownList绑定Dictionary泛型类 定义一个Dictionary泛型类 /// <summary>    /// 产品类型    /// </summary> ...

  2. DropDownList绑定及修改

    DropDownList绑定及修改 http://www.cnblogs.com/hulang/archive/2010/12/29/1920662.html   ? 1 2 3 4 5 6 7 8 ...

  3. 使用layui的form.on绑定select选中事件, form.on()不执行的原因分析

    使用layui的form.on绑定select选中事件中, form.on()不执行, 主要原因有 1, select标签中没有写lay_filter属性,用来监听 <select id=&qu ...

  4. [Asp.net]DropDownList改变默认选中项的两种方式

    引言 其实是不想总结这方面的内容,发现太简单了,可是在这上面也栽了跟头.所以还是记录一下吧,算是提醒自己,不要太看不起太基础的东西,有这种心理,是会载大跟头的. 一个例子 这里模拟一下最常用的一个例子 ...

  5. DropdownList绑定的两种方法

    动态绑定方法一:动态绑定数据库中的字段. SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();string strSQL ...

  6. DropDownList绑定多个字段值

    发觉这个问题还是挺多人问的,简单写几个例子: 假设现有1张表名为:XUDAXIA  , 该表里有2个字段:  NAME , GENDER 达到效果: 将这2个字段绑定到DropDownList的Lis ...

  7. C# DropDownList绑定文件夹

    首先创建一个类,类名称为FileControl, /// <summary> /// 获取制定文件夹下面的文件夹 /// </summary> /// <param na ...

  8. C# DropDownList绑定添加新数据的几种方法

    第一种:在前台手动绑定(适用于固定不变的数据项) <asp:DropDownList ID="DropDownList1" runat="server"& ...

  9. C# DropDownList绑定添加新数据的三种方法

    一.在前台手动绑定 <asp:DropDownList ID="DropDownList1" runat="server">    <asp: ...

随机推荐

  1. git:hook declined FATAL: W refs/heads DENIED by fallthru error

    hook declined FATAL: W refs/heads DENIED by fallthru error git提交代码时报错,网上查了,最终结果竟然是测试人员没有给我配置写的权限,配置了 ...

  2. cf C. Magic Formulas

    http://codeforces.com/contest/424/problem/C #include <cstdio> #include <cstring> #includ ...

  3. LeetCode_Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  4. LeetCode_Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. LeetCode_Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  6. mac OSX上eclipse adb无法识别(调试)小米的解决方案

    最近在Mac上开发安卓,用小米2a作为开发机,连上电脑后发现idea和eclipse真机调试的时候都提示USB device not found.经过一番google和百度,终于找到了解决方案,在这里 ...

  7. 【转】ubuntu下安装及设置FTP服务器!!

    原文网址:http://hujizhou.blog.51cto.com/514907/1290915 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...

  8. webapp设计注意事项

    色彩设计 美学相关的知识(色彩构成.平面构成等等)我就不再赘述了,相信从事此类行业的人员无人不知无人不晓了.这里简要说说WebApp设计中,色彩以及构图的特别之处吧. 首先是色彩.从事过广告和印刷业设 ...

  9. onethink 验证码二维码不显示的问题

    常规思路: 1 检查GD和FreeType.在项目根目录(index.php同级)下放一个php文件 <?php echo phpinfo(); ?> 访问此文件,查看GD和FreeTyp ...

  10. MySQL的备份和还原

    MySQL的备份和还原 备份:副本    RAID1,RAID10:保证硬件损坏而不会业务中止:        DROP TABLE mydb.tb1; 备份类型:        热备份.温备份和冷备 ...