ASP.NET——實現兩個下拉框動態聯動,asp.net下拉框

來源:互聯網
上載者:User

ASP.NET——實現兩個下拉框動態聯動,asp.net下拉框

引入:

      在網頁中,我們經常會遇到中的情況。首先在下拉框中選擇所在的省,選擇之後,第二個下拉框會自動載入出該省中的市。這樣設計極大的方便了使用者的尋找。那這是如何?的呢?



1、建立資料庫

“省”表


“市”表



2、添加控制項


3、兩個下拉框分別綁定資料來源

      protected void Page_Load(object sender, EventArgs e)        {            //判斷是否第一次進入頁面,如果是,則綁定資料庫;如果不是,則無需綁定。            if (!this.IsPostBack)            {                //綁定省                   SqlConnection con = DB.createConnection();                con.Open();                string cmdText = "select* from province";                SqlCommand cmd = new SqlCommand(cmdText, con);                SqlDataReader sdr = cmd.ExecuteReader();                this.DropDownList1.DataSource = sdr;                this.DropDownList1.DataTextField = "proName";//常值內容                this.DropDownList1.DataValueField = "proID"; //資料來源欄位                this.DropDownList1.DataBind();                sdr.Close();                //綁定市                string cmdCityText = "select* from city where proID=" + this.DropDownList1.SelectedValue;                SqlCommand cmdCity = new SqlCommand(cmdCityText, con);                sdr = cmdCity.ExecuteReader();                //剩下部分與綁定省類似,略                               //關閉串連                con.Close();            }        }  

到這裡,兩個文字框都已經載入到各自的資料。剩下的就是動態聯動了。

4、當我們更改第一個下拉框中的內容後,會觸發第一個文字框的SelectedIndexChanged事件。將第一個下拉框的proID(省的ID)作為參數,即可查到其市的內容。

具體代碼如下:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)        {            //省的ID            string proID = this.DropDownList1.SelectedValue;            SqlConnection con = DB.createConnection();            con.Open();            SqlCommand cmd = new SqlCommand("select * from city where proID=" + proID, con);            SqlDataReader sdr = cmd.ExecuteReader();            //綁定            this.DropDownList2.DataSource = sdr;            this.DropDownList2.DataTextField = "cityName";            this.DropDownList2.DataValueField = "cityID";            this.DropDownList2.DataBind();            sdr.Close();            con.Close();        }


       這樣,我們就可以實現動態聯動了。這樣的動態聯動,一般由多個下拉框組成一組菜單,比如上面用到的兩個下拉框。下拉式功能表之間有聯動的關係。當上級的選中項發生改變時,下級會根據上級中的選中項顯示相應的內容。

      雖然只是一個小技巧或者說是小的需求,但當資料量特別大時,它的功能就不可小視了。上次期末考試導考生的時候,可能只是一個頁面忽略了這個功能,結果導致工作量大大增加。

      用了動態聯動之後,當面對龐大的資料或複雜的分類時,頁面的載入速度也不會受到影響,也方便了使用者尋找。



aspnet下拉框二級聯動(怎實現)

用DropDownList

建立一個類:

#region 綁定DropDownList 控制項代碼
public static bool ControlClass(DropDownList CN, string SqlStr, string TF, string VF)
{
SqlConnection conn = DBConn.conn();
SqlCommand cmd = new SqlCommand(SqlStr, conn);

try
{
conn.Open();
SqlDataReader der = cmd.ExecuteReader();
CN.DataSource = der;
CN.DataTextField = TF;
CN.DataValueField = VF;
CN.DataBind();
return true;
}
catch
{
return false;
}
finally
{
conn.Close();
cmd.Dispon();
}
}
#endregion

代碼1:
調用類裡的 方法

if(!IsPostBack)
{
上面類名.ControlClass(DropDownList1, "select * from cdb_members", "userName", "uid");
// 把一級DropDwonList的AutoPostBacke 設定為true;
然後在設定第二個DropDowlist

代碼:

OperationDataClass.ControlClass(DropDownList2, "select * from cdb_members where uid = '" + this.ddlItems.SelectedValue + "'", "userName", "uid");
}

////首先資料庫這兩個表必須是關聯的
要是再不明白,可以去 #24

不給分就不夠意思了
 
ASPNET中,怎用c#語言實現兩個下拉式清單方塊的聯動?

用AJax,在你的頁面添加一個ScriptManager(放在最前面),和一個UpdatePannel,把DropDownList2放在UpdatePannel裡,添加UpdatePannel的Trigger,其中ControlID設為DropDownList1,Event設為SelecteIndex_Changed。然後在你的DropDownList1的SelecteIndex_Changed事件裡寫根據DropDownList1選定改變DropDownList2 內容的代碼。最後設定DropDownList1的AutoPostBack=True就可以了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.