单表实现省市区三级菜单

来源:互联网 发布:北京网络派出所电话 编辑:程序博客网 时间:2024/06/06 00:23

简单的省市区三级菜单功能实现:

用的是单表做的如图:


控件作用描述:

DropDownList1:省级菜单

DropDownList2:市级菜单

DropDownList3:区级菜单

protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                fun("0", DropDownList1);            }        }        private void fun(string id,DropDownList dd)        {            string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;            using (SqlConnection con = new SqlConnection(str))            {                con.Open();                using (SqlCommand cmd = con.CreateCommand())                {                    cmd.CommandText = "select A_Id,A_Name from T_Arear where A_ParentId=@id";                    cmd.Parameters.Add("@id", id);                    SqlDataAdapter ad = new SqlDataAdapter(cmd);                    DataTable table = new DataTable();                    ad.Fill(table);                    dd.DataSource = table;                    dd.DataTextField="A_Name";                    dd.DataValueField = "A_Id";                    dd.DataBind();                    if (id=="0")//页面初次加载显示顶置内容                    {                        ListItem list = new ListItem("--select--", "0");                        dd.Items.Insert(0, list);                    }                }            }        }        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)        {            if (DropDownList1.SelectedItem.Value != "0")//判断是否选择省            {                fun(DropDownList1.SelectedItem.Value, DropDownList2);                fun(DropDownList2.SelectedItem.Value, DropDownList3);            }            else//清空市、区下拉菜单内容            {                DropDownList2.Items.Clear();                DropDownList3.Items.Clear();            }        }        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)        {            fun(DropDownList2.SelectedItem.Value, DropDownList3);        }