前台 <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                &nbsp;<asp:DropDownList ID="DropDownList1" runat="server"
                     OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" >
                
                </asp:DropDownList>&nbsp;
                <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Data.mdb"
                    SelectCommand="SELECT [id], [type] FROM [ztype]"></asp:AccessDataSource>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" />
            </ContentTemplate>
        </asp:UpdatePanel>
后台 protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            this.DropDownList2.Items.Add("请选择");            this.DropDownList1.Items.Add("请选择");
        }
        DLTdataBind();    }
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ts = this.TextBox1.Text;
        string drotext=this.DropDownList2.SelectedValue;
        if (ts.Trim() != "" && ts.Trim() != null)
        {
            mycon = DB.CreateDB();
            mycon.Open();
            OleDbCommand cmd1 = new OleDbCommand("insert into type(type,typeid) values(" + ts + "," + drotext + ")", mycon);
            cmd1.ExecuteNonQuery();
            Response.Write("<script>alert('数据添加成功');</script>");
            mycon.Close();
        }
        //DLTdataBind(drotext);
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
        string id = this.DropDownList1.SelectedValue;
        DLTdataBind(id);
        this.TextBox1.Text = id;    }
    protected void DLTdataBind( string id)
    {        //this.DropDownList2.Items.Clear();
        DataSet ds = new DataSet();
        this.TextBox1.Text = id;
        mycon = DB.CreateDB();
        mycon.Open();
        OleDbCommand cmd1 = new OleDbCommand("select * from type where typeid=" + id, mycon);
        OleDbDataAdapter oda = new OleDbDataAdapter(cmd1);
        oda.Fill(ds, "customer");        //this.TextBox1.Text = id;
        this.DropDownList2.DataValueField = "id";
        this.DropDownList2.DataTextField = "type";
        this.DropDownList2.DataSource = ds;
        this.DropDownList2.DataBind();
        this.mycon.Close();
      
       
    }
    protected void DLTdataBind()
    {        this.DropDownList1.Items.Clear();
        DataSet ds = new DataSet();        mycon = DB.CreateDB();
        mycon.Open();
        OleDbCommand cmd1 = new OleDbCommand("select * from ztype", mycon);
        OleDbDataAdapter oda = new OleDbDataAdapter(cmd1);
        oda.Fill(ds, "customer");        //this.TextBox1.Text = id;
        this.DropDownList1.DataValueField = "id";
        this.DropDownList1.DataTextField = "type";
        this.DropDownList1.DataSource = ds;
        this.DropDownList1.DataBind();
        this.mycon.Close();
    }
现在问题是dropdownlist改变第一次的时候可以去到值,第二次改变的时候取到的还是第一次的值,怎么办?

解决方案 »

  1.   

    又执行了一次绑定,你加个if (!IsPostBack)
      

  2.   

    1,新建js文件,代码如下://设置市,选项function Setcity(ary)
    {//将获取的存储市名称及编码的字符串进行分离,//每个数组元素中存储一个市的名称及编码
        var arycity = new Array();
        arycity = ary.toString().split("|");
       
    //    var list = document.getElementById("ddlcity");
    //    list.options.length = 0;//清空ddlcity选项
        $("#ddlcity").empty();
        for(i=0;i<arycity.length-1;i++)
        {//将数组arycity中的每个数组元素进行分离//分离出市的名称及市的编码 
              var citystr = null;
              citystr = arycity[i];
              var ary = new Array();
              ary = citystr.toString().split(",");//新增dropdownlist的选项
            var newoption = document.createElement("OPTION");
            newoption.text = ary[0];
            newoption.value= ary[1];
            document.getElementById("ddlcity").options.add(newoption);
        }
    }//jquery异步调用City.ashx
    $(document).ready(function(){$("#ddlProvince").change(function(){//参数传递的两种方法//1,var code = "procode=" + $("#ddlProvince").val();
    //2,data:{procode:code},
         //var code = "procode=" + $("#ddlProvince").val();
            var code =$("#ddlProvince").val();
             $.ajax({
            type:"POST",
            url:"City.ashx",
            data:{procode:code},
            success:Setcity
        })
    })})2,新建ashx文件,代码如下:public class City : IHttpHandler {
       
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";//接收参数
            string code = context.Request.Params["procode"].ToString().Trim();
            //string strcity = GetCity(code);
            //context.Response.Write("Hello World");
            string stritem = GetCity(code);
            context.Response.Write(stritem);
        }    public string GetCity(string procode)
        {
            List<YD.Model.City> citylist = new YD.BLL.City().GetList("*", " provinceId='"+procode+"'","name asc");
            int length = citylist.Count;
            string strary = "";
            for (int i = 0; i < length; i++)
            {
               // strary[i] = citylist[i].name + "|" + citylist[i].code;
                strary += citylist[i].name + "," + citylist[i].code + "|";
            }
           
            return strary;
        }
       
        public bool IsReusable {
            get {
                return false;
            }
        }}前台代码://记得引用jquery包<script src="../js/jquery-1.2.6.pack.js" type="text/javascript"></script><script src="Js/getCity.js" type="text/javascript"></script>                <td>
                        <asp:DropDownList ID="ddlProvince" runat="server" CssClass="normal_sele">
                        </asp:DropDownList>
                        <asp:DropDownList ID="ddlcity" runat="server" CssClass="normal_sele">
                       
                        </asp:DropDownList>
                    </td>