asp.net DropDownList 三級(jí)聯(lián)動(dòng)下拉菜單實(shí)現(xiàn)代碼
更新時(shí)間:2009年12月05日 23:29:19 作者:
asp.net DropDownList 三級(jí)聯(lián)動(dòng)下拉菜單效果代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
if (!IsPostBack)
{
//一級(jí)分類(lèi)列表
this.DropDownList1.DataSource = dsbb.SelectSubjct1();
this.DropDownList1.DataTextField = "cName";
this.DropDownList1.DataValueField = "Ccode";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,new ListItem("請(qǐng)選擇一級(jí)分類(lèi)","0"));
this.DropDownList8.Items.Insert(0, new ListItem("請(qǐng)選擇二級(jí)分類(lèi)", "0"));
this.DropDownList9.Items.Insert(0,new ListItem ("請(qǐng)選擇三級(jí)分類(lèi)","0"));
//二級(jí)分類(lèi)列表
}
/// <summary>
/// 綁定二級(jí)分類(lèi)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
libs.Database.Dbbase dbb = new libs.Database.Dbbase();
if (Convert.ToInt32(this.DropDownList1.SelectedValue) == 0) //清除列表內(nèi)容
{
this.DropDownList8.Items.Clear();
this.DropDownList8.Items.Insert(0, new ListItem("請(qǐng)選擇二級(jí)分類(lèi)", "0"));
this.DropDownList9.Items.Clear();
this.DropDownList9.Items.Insert(0, new ListItem("請(qǐng)選擇三級(jí)分類(lèi)", "0"));
}
else //二級(jí)分類(lèi)列表
{
this.DropDownList8.DataSource = dbb.Selectsubjct2(this.DropDownList1.SelectedValue.Substring(0,2));
this.DropDownList8.DataTextField = "cName";
this.DropDownList8.DataValueField = "Ccode";
this.DropDownList8.DataBind();
this.DropDownList8.Items.Insert(0,new ListItem ("請(qǐng)選擇二級(jí)分類(lèi)","0"));
this.DropDownList9.Items.Clear();//清除第三分類(lèi)
this.DropDownList9.Items.Insert(0, new ListItem("請(qǐng)選擇三級(jí)分類(lèi)", "0"));
}
}
/// <summary>
/// 綁定三級(jí)分類(lèi)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
libs.Database.Dbbase dbase = new libs.Database.Dbbase();
this.DropDownList9.DataSource = dbase.selectsubject3(this.DropDownList8.SelectedValue.Substring(0,4));
this.DropDownList9.DataTextField = "cName";
this.DropDownList9.DataValueField = "Ccode";
this.DropDownList9.DataBind();
this.DropDownList9.Items.Insert(0,new ListItem("請(qǐng)選擇三級(jí)分類(lèi)","0"));
}
Dbbase.cs頁(yè):
復(fù)制代碼 代碼如下:
/// <summary>
/// 查詢(xún)一級(jí)欄目
/// </summary>
/// <returns></returns>
public DataSet SelectSubjct1()
{
string con = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = new SqlConnection(con);
string sqlstr = "SELECT kndid, Ccode, cName, cLevel FROM kind WHERE cLevel = 1";
DataSet dst = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sqlstr,conn);
try
{
sda.Fill(dst);
return dst;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 查詢(xún)二級(jí)欄目?jī)?nèi)容
/// </summary>
/// <param name="ccode"></param>
/// <returns></returns>
public DataSet Selectsubjct2(string ccode)
{
string conn1 = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = new SqlConnection(conn1);
string sqqq = "select kndid,Ccode,cName,cLevel from kind where cLevel = 2 and Ccode like '" + ccode + "%'";
DataSet dss = new DataSet();
SqlDataAdapter sdd = new SqlDataAdapter(sqqq,conn);
try
{
sdd.Fill(dss);
return dss;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 查詢(xún)?nèi)?jí)欄目?jī)?nèi)容
/// </summary>
/// <param name="cde"></param>
/// <returns></returns>
public DataSet selectsubject3(string cde)
{
string conn2 = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = new SqlConnection(conn2);
string sqq = "select kndid,Ccode,cName,cLevel from kind where cLevel = 3 and Ccode like '" + cde + "%'";
DataSet dst = new DataSet();
SqlDataAdapter sdaa = new SqlDataAdapter(sqq,conn);
try
{
sdaa.Fill(dst);
return dst;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
注意:DropDownList1_SelectedIndexChanged 事件,AutoPostBack="True"
相關(guān)文章
ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶(hù)資料的修改和刪除3(七)
這篇文章主要為大家詳細(xì)介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)之用戶(hù)資料的修改和刪除,感興趣的小伙伴們可以參考一下2016-08-08
Asp.net回調(diào)技術(shù)Callback學(xué)習(xí)筆記
這篇文章主要記錄了Asp.net回調(diào)技術(shù)Callback的一些知識(shí),感興趣的朋友可以參考下2014-08-08
ASP.NET中Dictionary基本用法實(shí)例分析
這篇文章主要介紹了ASP.NET中Dictionary基本用法,結(jié)合實(shí)例形式分析了Dictionary的基本功能、使用步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
ASP.NET中實(shí)現(xiàn)文件的保護(hù)性下載基礎(chǔ)篇
許多時(shí)候,我們需要在因特網(wǎng)上提供文件下載服務(wù),但是又要防止未經(jīng)授權(quán)的下載,這時(shí)該怎么辦?本文將為讀者詳細(xì)介紹一種使用ASP.NET實(shí)現(xiàn)的HTTP處理程序的解決方案。2011-02-02
Asp.net控制Tomcat啟動(dòng)關(guān)閉的實(shí)現(xiàn)方法
近日有個(gè)項(xiàng)目客戶(hù)要求能自己配置相關(guān)權(quán)限。由于歷史原因這個(gè)項(xiàng)目采用的是公司以前的權(quán)限系統(tǒng)2012-01-01

