如何将数据源与下拉列表(DropDownList)进行绑定?
- 内容介绍
- 文章标签
- 相关推荐
本文共计270个文字,预计阅读时间需要2分钟。
csharppublic bool ecDropDownList(DropDownList DDL, string sqlstr3, string DTF, string DVF){ // 创建数据库连接 SqlConnection con=this.getco(); bool success=false;
try { // 打开连接 con.Open();
// 执行SQL语句 SqlCommand cmd=new SqlCommand(sqlstr3, con); SqlDataReader reader=cmd.ExecuteReader();
// 清空现有项 DDL.Items.Clear();
// 读取数据并添加到下拉列表中 while (reader.Read()) { DDL.Items.Add(reader[DTF].ToString(), reader[DVF].ToString()); }
success=true; } catch (Exception ex) { // 处理异常 Console.WriteLine(Error: + ex.Message); } finally { // 关闭连接 if (con.State==ConnectionState.Open) { con.Close(); } }
return success;}
publicboolecDropDownList(DropDownListDDL,stringsqlstr3,stringDTF,stringDVF){创建数据库连接SqlConnpublic bool ecDropDownList(DropDownList DDL, string sqlstr3, string DTF, string DVF){//创建数据库连接SqlConnection con this.getcon();//打开数据库连接con.Open();//定义并初使化数据适配器SqlDataAdapter mydataadapter new SqlDataAdapter(sqlstr3,con);//创建一个数据集mydatasetDataSet mydataset new DataSet();//将数据适配器中的数据填充到数据集中mydataadapter.Fill(mydataset);DDL.DataSource mydataset;DDL.DataTextField DTF;DDL.DataValueField DVF;try{DDL.DataBind();return true;}catch{return false;}finally{//关闭数据库连接con.Close();}}
与之相关联的DropDownList的数据绑定
public void GetDropDownList(){dataconn.ecDropDownList(ddlSelQue, "select * from tb_TaoTi where LessonID" ddlSelLess.SelectedValue.ToString() "", "Name", "ID");}
本文共计270个文字,预计阅读时间需要2分钟。
csharppublic bool ecDropDownList(DropDownList DDL, string sqlstr3, string DTF, string DVF){ // 创建数据库连接 SqlConnection con=this.getco(); bool success=false;
try { // 打开连接 con.Open();
// 执行SQL语句 SqlCommand cmd=new SqlCommand(sqlstr3, con); SqlDataReader reader=cmd.ExecuteReader();
// 清空现有项 DDL.Items.Clear();
// 读取数据并添加到下拉列表中 while (reader.Read()) { DDL.Items.Add(reader[DTF].ToString(), reader[DVF].ToString()); }
success=true; } catch (Exception ex) { // 处理异常 Console.WriteLine(Error: + ex.Message); } finally { // 关闭连接 if (con.State==ConnectionState.Open) { con.Close(); } }
return success;}
publicboolecDropDownList(DropDownListDDL,stringsqlstr3,stringDTF,stringDVF){创建数据库连接SqlConnpublic bool ecDropDownList(DropDownList DDL, string sqlstr3, string DTF, string DVF){//创建数据库连接SqlConnection con this.getcon();//打开数据库连接con.Open();//定义并初使化数据适配器SqlDataAdapter mydataadapter new SqlDataAdapter(sqlstr3,con);//创建一个数据集mydatasetDataSet mydataset new DataSet();//将数据适配器中的数据填充到数据集中mydataadapter.Fill(mydataset);DDL.DataSource mydataset;DDL.DataTextField DTF;DDL.DataValueField DVF;try{DDL.DataBind();return true;}catch{return false;}finally{//关闭数据库连接con.Close();}}
与之相关联的DropDownList的数据绑定
public void GetDropDownList(){dataconn.ecDropDownList(ddlSelQue, "select * from tb_TaoTi where LessonID" ddlSelLess.SelectedValue.ToString() "", "Name", "ID");}

