Webfrom --中國直轄市三區聯動

效果圖:數據庫

(一)數據庫操做方法:

public class mydb
{
    private MyDBDataContext context = new MyDBDataContext(); public List<ChinaStates> Select(string AreaCode)//將中國的表所有查詢
 { return  context.ChinaStates.Where(p=>p.ParentAreaCode==AreaCode).ToList(); } 
}
(二) 操做方法
public partial class yemian : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)//顯示界面 { if (!IsPostBack) { fangfa(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//根據第一個來操做第二個和第三個
    {
        string code = DropDownList1.SelectedItem.Value;
        List<ChinaStates> diqu1 = new mydb().Select(code);
        DropDownList2.DataSource = diqu1;
        DropDownList2.DataTextField = "AreaName";
        DropDownList2.DataValueField = "AreaCode";
        DropDownList2.DataBind();

        List<ChinaStates> diqu3= new mydb().Select(code);
        DropDownList3.DataSource = diqu3;
        DropDownList3.DataTextField = "AreaName";
        DropDownList3.DataValueField = "AreaCode";
        DropDownList3.DataBind();

    }
    private void fangfa()//寫了一個方法 { List<ChinaStates> list = new mydb().Select("0001"); DropDownList1.DataSource = list;//制定數據源 DropDownList1.DataTextField = "AreaName"; DropDownList1.DataValueField = "AreaCode"; DropDownList1.DataBind();//最後綁定一下 List<ChinaStates> shi = new mydb().Select("11"); DropDownList2.DataSource = shi; DropDownList2.DataTextField = "AreaName"; DropDownList2.DataValueField = "AreaCode"; DropDownList2.DataBind(); List<ChinaStates> diqu = new mydb().Select("1101"); DropDownList3.DataSource = diqu; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)//根據第二個改變第三個 { string code = DropDownList2.SelectedItem.Value; List<ChinaStates> diqu2 = new mydb().Select(code); DropDownList3.DataSource = diqu2; DropDownList3.DataTextField = "AreaName"; DropDownList3.DataValueField = "AreaCode"; DropDownList3.DataBind(); }
}


點擊改變事件必定要將AutoPostBack 的屬性改爲true
相關文章
相關標籤/搜索