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
相关文章
相关标签/搜索