Posted on 2007-04-22 14:38
停留的風 閱讀(1376)
評論(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();