一个简单的无刷新DropDownList及联 写个小例子 把DropDownList放入UpdatePanel 中

    //省份数据的列表

    string[][] Provins;

    //城市数据的列表

    string[][] Citys;

    protected void Page_Load(object sender, EventArgs e)
    {
        //手动设置省份列表的内容
       Provins = new string[][]
       {
       new string[]{"1","四川"},
       new string[]{"3","湖北"},
       new string[]{"2","湖南"}
     
       };
        //手动设置城市列表的内容
       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++)
            {
                string v = Provins[i][1].ToString();
                DropDownList1.Items.Add(new ListItem(Provins[i][1], Provins[i][0]));
            }
            //循环给城市下拉列表赋值
            for (int i = 0; i < Citys.Length; i++)
            {
                string c = Citys[i][0].ToString();
                DropDownList2.Items.Add(new ListItem(Citys[i][0], Citys[i][1]));
            }
        }
    }

   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 (DropDownList1.SelectedValue.Equals(Citys[i][1]))
            {
                string a = DropDownList1.SelectedValue.ToString();
                DropDownList2.Items.Add(Citys[i][0]);
            }
        }
    }

以上代码很简单,轻松的就实现无刷新DropDownList及联  比起写一大堆脚本,为了保持状还要态返回参数等等.简单又实用

 posted on 2008-10-23 00:29  飞虎  阅读(719)  评论(3编辑  收藏  举报