用ajax实现的局部刷新,当点击按钮往数据库中添加下拉列表的项,数据库中数据已经更新,但是下拉列表中的项没有更新,把页面关掉再打开却得到了更新,请问是什么原因?

解决方案 »

  1.   

    你的AJAX是怎么写的是关键。
    你这样问太简单了,没有办法回答
      

  2.   

    点button后,在把数据bind()
    不过你问的比较笼统了,
      

  3.   

    代码比较多,望各位大虾包含,以下为前台代码,关于xmlhttprequest对象如何获得小弟在此就只考虑ie浏览器
                                var xmlHttp = false;

    //创建XMLHttpRequest对象函数
    function GetXMLHttpRequest()
    {
    // 由于 XMLHttpRequest 对象在各个浏览器中实现机制不同,所以要综合考虑
        var xmlHttp = false;
        //如果是IE浏览器
        if(window.ActiveXObject)
        {
        try 
        {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
        try 
        {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e2) 
        {
        xmlHttp = false;
        }
        }     
        }
        return xmlHttp;
    }

    function getSecondCategory()
    {
    var RootCategory = document.getElementById("<%=RootCategory.ClientID%>");
    var hidden=document.getElementById("<%=HiddenArea.ClientID%>");
    hidden.value="";
    hidden.value=RootCategory.options[RootCategory.selectedIndex].value;
    //if (RootCategory.Items == null)
    //return;
                    xmlHttp = GetXMLHttpRequest();
    // 构造将XMLHttpRequest对象将请求的服务器端的URL字符串
    //alert(RootCategory.options[RootCategory.selectedIndex].value);
    var url = "GetSecondCategory.aspx?CategoryID="+RootCategory.options[RootCategory.selectedIndex].value;
    // 建立与服务器端的连接
    xmlHttp.open("GET", url, true);
    // 设置回调函数
    xmlHttp.onreadystatechange = callBack_getSecondCategory;
    // 发送请求
    xmlHttp.send(null);
    }

    function callBack_getSecondCategory()
    {
        var f=document.getElementById("<%=SecondCategory.ClientID%>");
        var lbl=document.getElementById("lbl1");
        var lbl2=document.getElementById("lbl2");
        if (4 == xmlHttp.readyState) 
    {
        if(xmlHttp.status==200)
    {
    var list=xmlHttp.responseText;
    var classList=list.split("|");
    lbl2.innerHTML=classList.length;
    f.options.length=1;
    for(var i=0;i<classList.length;i++)
    {
    var tmp=classList[i].split(",");
    f.add(new Option(tmp[1],tmp[0]));
    }
    }
    else
    {
    alert("您所请求的页面有异常。");
    }
    }
                    else
    {
    //    alert("else");
        //lbl.innerHTML ="yyy"; 
    }
    }

        function getThirdCategory()
    {
    var SecondCategory = document.getElementById("<%=SecondCategory.ClientID%>");
    var hidden=document.getElementById("<%=HiddenArea.ClientID%>");
    hidden.value="";
    hidden.value=SecondCategory.options[SecondCategory.selectedIndex].value;
    //if (RootCategory.Items == null)
    //return;
                    xmlHttp = GetXMLHttpRequest();
    // 构造将XMLHttpRequest对象将请求的服务器端的URL字符串
    var url = "GetSecondCategory.aspx?CategoryID="+SecondCategory.options[SecondCategory.selectedIndex].value;
    // 建立与服务器端的连接
    xmlHttp.open("GET", url, true);
    // 设置回调函数
    xmlHttp.onreadystatechange = callBack_getThirdCategory;
    // 发送请求
    xmlHttp.send(null);
    }

    function callBack_getThirdCategory()
    {
        var f=document.getElementById("<%=ThirdCategory.ClientID%>");
        var lbl=document.getElementById("lbl2");
        if (4 == xmlHttp.readyState) 
    {
        if(xmlHttp.status==200)
    {
        lbl.innerHTML =xmlHttp.responseText;
    var list=xmlHttp.responseText;
    var classList=list.split("|");
    f.options.length=1;
    for(var i=0;i<classList.length;i++)
    {
    var tmp=classList[i].split(",");
    f.add(new Option(tmp[1],tmp[0]));
    }
    }
    else
    {
    alert("您所请求的页面有异常。");
    }
    }
                    else
    {
        lbl.innerHTML ="yyy"; 
    }
    }

    function getParentCategoryName()
    {
        var ThirdCategory=document.getElementById("<%=ThirdCategory.ClientID%>");
    var hidden=document.getElementById("<%=HiddenArea.ClientID%>");
    hidden.value="";
    hidden.value=ThirdCategory.options[ThirdCategory.selectedIndex].value;     
    }后台代码
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "管理员-->商品类别";
            bindRootCategory();
            RootCategory.Items.Insert(0, new ListItem("请选择分类", "0"));
            //SecondCategory.Disabled = true;
            //ThirdCategory.Disabled = true;
            this.btnDelete.Attributes.Add("onclick", "return confirm('您确定要删除吗?')");
        }    protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = System.Configuration.ConfigurationManager.AppSettings["Theme"];
        }    protected void bindRootCategory()
        {
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Category where ParentCategoryID=0", con);
                try
                {
                    con.Open();
                    RootCategory.DataSource = cmd.ExecuteReader();
                    RootCategory.DataTextField = "CategoryName";
                    RootCategory.DataValueField = "CategoryID";
                    RootCategory.DataBind();
                }
                catch (Exception)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "yy", "<script>alert('系统错误!')</script>");
                }
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //lblMessage.Text = HiddenArea.Value;
            //Response.Write(RootCategory.SelectedIndex);
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("insert into Category(ParentCategoryID,CategoryName) values(@ParentCategoryID,@CategoryName)", con);
                cmd.Parameters.AddWithValue("@CategoryName", txtNewCategoryName.Text.Trim());
                cmd.Parameters.AddWithValue("@ParentCategoryID",HiddenArea.Value);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "xx", "<script>alert('操作成功!')</script>");
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "yy", ex.Message);
                }
            }
            Server.Transfer("Category.aspx");
        }    protected void btnDelete_Click(object sender, EventArgs e)
        {
            lblMessage.Text = String.Empty;
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("delete from Category where CategoryID=@CategoryID", con);
                cmd.Parameters.AddWithValue("@CategoryID", HiddenArea.Value);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    lblMessage.Text = "删除成功!";
                }
                catch(Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
            }
        }
      

  4.   

    服务器处理页面GetSecondCategory.aspx.cs
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            //System.Threading.Thread.Sleep(1000);
            String SecondCategory = getSecondCategory();        // 发送验证结果
            //    Response.Clear();
            //    Response.ContentType = "text/xml";
            Response.ContentType = "text/html";
            Response.Write(SecondCategory.ToString());
            Response.Flush();
            Response.End();
        }    protected String getSecondCategory()
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("CategoryID", typeof(String)));
            table.Columns.Add(new DataColumn("CategoryName", typeof(String)));
            String CategoryName=String.Empty;
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("select CategoryID,CategoryName from Category where ParentCategoryID=@CategoryID", con);
                cmd.Parameters.AddWithValue("@CategoryID", Request.QueryString["CategoryID"]);
                try
                {
                    con.Open();
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    adp.Fill(ds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
                    {
                        if (i == ds.Tables[0].Rows.Count - 1)
                        {
                            CategoryName += ds.Tables[0].Rows[i]["CategoryID"].ToString() + "," + ds.Tables[0].Rows[i]["CategoryName"].ToString();
                        }
                        else
                        {
                            CategoryName += ds.Tables[0].Rows[i]["CategoryID"].ToString() + "," + ds.Tables[0].Rows[i]["CategoryName"].ToString() + "|";
                        }
                    }
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            return CategoryName;
        }