其时你上面所写的代码就已经是绑定了,只不过不是很完全。当然,绑定也可以完全从后台来写,eg:
sda.Fill(dataset1);//相应从数据库中读并填充到dataset中你已经会了,这里略过
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextFild="你的字段名1";
this.DropDownList1.DataValueFile="你的字段名2";
this.DropDownList1.DataBind();另一种方法就是获得sqldatareader对象,然后:
while(sdr.Read())
{
this.DropDownList1.Items.Add(new ListItem(sdr.GetValue(0).ToString(),Sdr.GetValue(1).ToString()));
}
以上两种方法都是可以用来绑定dropdownlist的
sda.Fill(dataset1);//相应从数据库中读并填充到dataset中你已经会了,这里略过
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextFild="你的字段名1";
this.DropDownList1.DataValueFile="你的字段名2";
this.DropDownList1.DataBind();另一种方法就是获得sqldatareader对象,然后:
while(sdr.Read())
{
this.DropDownList1.Items.Add(new ListItem(sdr.GetValue(0).ToString(),Sdr.GetValue(1).ToString()));
}
以上两种方法都是可以用来绑定dropdownlist的
解决方案 »
- .net 动态创建二级域名
- 问个CSS兼容的问题 (办法想尽未果)
- asp.net网页兼容IE8
- 如何实现WinForm的FolderBrowerDialog
- System.InvalidCastException: 从类型“OleDbDataReader”到类型“String”的转换无效。
- 数据如何同时放进主表和明细表?
- string.Replace(string newValue, string oldValue)能忽略大小写替换吗?
- 请问错在哪里?立即给分!string[] SubSelectCount = Request.Form["SubSelect"].ToString().Split(',');
- 关于在HtmlTable中动态添加行及控件的问题,请各位帮忙!!!
- 定时发送一定的内容给某个电子邮箱,怎么做?给个思路,各位
- 再次请教,一个简单的JS问题。(有好人吗,我无分可加了)
- 大师救我!!!
<appSettings>
<add key="ds" value="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Hospital;connect timeout=120" />
</appSettings>
在.cs文件中来绑定DropDownListusing System.Data.SqlClient;
using System.Configuration;
protected System.Data.SqlClient.SqlConnection cn;
protected System.Data.SqlClient.SqlCommand cm;
protected System.Data.SqlClient.SqlDataReader dr;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
bindlistdepart();
}
}
private void bindlistdepart()
{
cn= new SqlConnection(ConfigurationSettings.AppSettings["ds"]);
string strSQL="select * from department order by depart_id";
cm = new SqlCommand(strSQL,cn);
cn.Open();
dr = cm.ExecuteReader();
DropDownList1.DataSource=dr;
DropDownList1.DataTextField="depart_name";
DropDownList1.DataValueField="depart_id";
DropDownList1.DataBind();
cn.Close();
}