$('#cid').combobox({
url:'showClass.do',
valueField:'id',
textField:'text'
})
然后,控制层返回的数据应该是JSON格式的数据,例如
[{"id":1, "text":"text1"},{ "id":2, "text":"text2"}]
1、在HBuilder软件中创建web项目,并在web项目指定的目录下创建静态页面select.html。
2、引入EasyUI插件核心css和js文件,并引入jQuery核心js文件。
3、下面开始编写ComboBox容器,该控件利用的是select标签元素构成的,设置ComboBox样式,如下图所示。
4、编辑js代码,获取ComboBox选中的值和文本,利用getValue和getText方法分别获取值和文本。
以users表为例,有三个字段,自增长的编号id,int类型;名称name,nvarchar类型,密码pwd,nvarchar类型首先在vs2005中引入using System.Data.SqlClient命名空间
/// 查询
/// </summary>
/// <returns></returns>
public DataTable Select()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open()
string sql = "select * from users"
SqlCommand cmd = new SqlCommand(sql, conn)
SqlDataAdapter sda = new SqlDataAdapter(cmd)
DataTable dt = new DataTable()
sda.Fill(dt)
conn.Close()
cmd.Dispose()
return dt
}
方法写好后,在form窗体中拖一个comboBox,然后在Load方法中
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = Select()//绑定数据
comboBox1.DisplayMember="name"//下拉列表中显示的是你数据库中name的值
comboBox1.ValueMember ="id"//这个一般绑定的是id,增加删除之用。这个属性也可不设
}
这样一运行,comboBox中就会显示数据了!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)