Posted on 2007-04-22 14:38
停留的风 阅读(1373)
评论(1) 编辑 收藏 所属分类:
.NET技巧特辑
public void dplistBind()
{
this.DropDownList1.DataTextField="";
SqlConnection con=DB.createCon();
con.Open();
string strName="select * from tch_gaikuang where zaijian=0";
SqlCommand cmd=new SqlCommand(strName,con);
SqlDataReader sdr=cmd.ExecuteReader();
this.DropDownList1.DataSource=sdr;
this.DropDownList1.DataTextField="name";
this.DropDownList1.DataValueField="name";
this.DropDownList1.DataBind();
sdr.Close();
}
以ASP.NET为例实现省市区的三级联动
public void getDdlInfo(DropDownList province,DropDownList city,DropDownList town,Page myPage)
{ //初始化省 string sql="select 序号,省名 from 省表";
SqlCommand comm=new SqlCommand(sql,conn);
province.Items.Clear();
province.Items.Add(new ListItem("-=请选择=-","0"));
conn.Open();
SqlDataReader dr=comm.ExecuteReader();
while(dr.Read())
{
province.Items.Add(new ListItem(dr[1].ToString(),dr[0].ToString()));
}
dr.Close();
//初始化市 city.Items.Add(new ListItem("-=请选择=-","0"));
//初始化区 to
含类的下拉框
public void Goverment_dplistBind()
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionString"];//从Web.config中读取
SqlConnection con =new SqlConnection(strconn);
con.Open();
string strSql="select * from links where link_Class='政府部门'";
SqlCommand cmd=new SqlCommand(strSql,con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddl_Goverment.DataSource=sdr;
this.ddl_Goverment.DataTextField="link_Name";
this.ddl_Goverment.DataValueField="link_Name";
this.ddl_Goverment.DataBind();
//下列做的初始化工作,如果不这样就会使得数据库中的一项被初始化操作覆盖掉,两种方法
this.ddl_Goverment.Items.Insert(0,"政府部门");
//this.ddl_Goverment.Items.Add(new ListItem("政府部门",""));
//this.ddl_Goverment.SelectedIndex=this.ddl_Goverment.Items.IndexOf(this.ddl_Goverment.Items.FindByText("政府部门"));
sdr.Close();