级联菜单是这么实现的?省和市都是从数据库里读取的,选择省,就显示这个省包含的市,请各位告示指教,谢了...........

解决方案 »

  1.   

    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
            SqlDataAdapter da = new SqlDataAdapter("select state from user_item group by state", con);        //                DataSet ds = new DataSet("State");
            //                da.Fill(ds);
            //                this.DropDownList1.DataSource = ds.Tables[0];
            //                this.DropDownList1.DataTextField = "state";
            //                this.DropDownList1.DataValueField = "state"; 
            //                this.DropDownList1.DataBind();  
            //                this.DropDownList1.Attributes.Add("onchange","load()");
            ///////////////        DataSet ds = new DataSet();
            da.Fill(ds);
            this.DropDownList1.DataSource = ds;
            this.DropDownList1.DataTextField = "state";
            this.DropDownList1.DataValueField = "state";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, "请选择");
            }      
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (this.DropDownList1.SelectedValue != "0")
            //{
                SqlConnection scon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
                SqlDataAdapter da1 = new SqlDataAdapter("select address from user_item where state = '" + this.DropDownList1.SelectedValue + "' order by address", scon);
                DataSet ds1 = new DataSet("address");
                da1.Fill(ds1);
                this.DropDownList2.DataSource = ds1;
                this.DropDownList2.DataTextField = "address";
                this.DropDownList2.DataValueField = "address";
                this.DropDownList2.DataBind();
            //}
        }
    完整的代码!
      

  2.   

    updatepanel 怎么实现级联菜单
      

  3.   

    首先我们还是创建一个ASP.NET AJAX的项目。在“工具栏”点击“文件”,在弹出的菜单里选择“新建”项,在子菜单中选择“网站”,打开如图15.30所示“新建网站”对话框:图15.30点击“确定”按钮后创建站点。站点创建完成后默认建立了Default.aspx这个页面,并且将这个页面在编辑器打开。我们在Default.aspx这个页面上拖入一个UpdatePanel控件和两个DropDownList控件,第一个DropDownList控件需要在“任务菜单”中设置“AutoPostBack”属性为“true”,以便引发数据刷新的事件。设置好的页面如图15.29所示:图15.29然后在UpdatePabel控件中添加Trigger。在属性窗口找到Triggers,点击“...”按钮,打开如图15.30所示的集合编辑器:图15.30在集合编辑器添加两个Trigger项,第一个设置关联控件为DropDownList1,事件名称为“SelectedIndexChanged”,第二个Trigger设置关联控件为DropDownList2,事件名同样是“SelectedIndexChanged”。我们双击页面进入代码编辑器,在Page_Load事件中给两个DropDownList控件绑定值。下面是页面的代码。public partial class _Default : System.Web.UI.Page {   //省份数据的列表   string[][] Provins;   //城市数据的列表   string[][] Citys;   protected void Page_Load(object sender, EventArgs e)   {       //手动设置省份列表的内容       Provins = new string[][]{       new string[]{"1","四川"},       new string[]{"2","湖南"},       new string[]{"3","湖北"}   };   //手动设置城市列表的内容   Citys = new string[][]   {       new string[]{"成都","1"},       new string[]{"绵阳","1"},       new string[]{"德阳","1"},       new string[]{"长沙","2"},       new string[]{"湘潭","2"},       new string[]{"武汉","3"},       new string[]{"荆州","3"},   };   if (!IsPostBack)   {       //循环给省份下拉列表赋值       for (int i = 0; i < Provins.Length; i++)       {           DropDownList1.Items.Add(Provins[i][1]);       }       //循环给城市下拉列表赋值       for (int i = 0; i <Citys.Length; i++)       {           DropDownList2.Items.Add(Citys[i][0]);       }   }}接下来我们继续给第一个DropDownList控件设定“SelectedIndexChanged”事件的处理方法。在DropDownList1控件的属性窗口里点击“闪电”按钮,找到“SelecedIndexChaneged”这一项,设置为“FlushList”,显示如图15.31所示:图15.31我们需要在这个事件的处理方法里根据第一个DropDownList控件选定的值重新设定第二个DropDownList控件绑定的数据,下面是这个方法的代码:protected void FlushList(object sender, EventArgs e){   //先清除第二个DropDownList控件中的所有项   DropDownList2.Items.Clear();   //循环城市列表   for (int i = 0; i < Citys.Length; i++)   {       string id = Citys[i][1];       //挑选和省份ID相同的城市添加到第二个DropDownList控件中       if (Provins[int.Parse(id) - 1][1].Equals(DropDownList1.SelectedValue))       {           DropDownList2.Items.Add(Citys[i][0]);       }   }}最后我们按F5键执行页面,显示结果如图15.32所示:图15.32在选定省份之后就会刷新城市的信息。这里我们可以看到页面并没有提交,而只是DropDownList控件稍微闪烁了一下更新了数据。相比用Js实现的那个版本,无论从代码可读性和适用性上都得到了很大的提高。