weixin_33698823 2017-03-02 04:33 采纳率: 0%
浏览 45

如何通过ajax追加div

i'm trying to retrieve values from database after that how can i pass those values from that static method and append to div in ajax ,Here is the C# code for that

[WebMethod]
    [ScriptMethod]
    public static string[] Cdetails(Cmpny cmpny)
    {
        StringBuilder html = new StringBuilder();
        string strCname = cmpny.Cname;
        string strCvalue= cmpny.Cvalue;
        string strLvl = cmpny.Clevel;
        int intLvl = Convert.ToInt32(strLvl);
        List<string> company = new List<string>();
        if (intLvl == 1)
        {


           SqlConnection conn = new SqlConnection(HttpContext.Current.Session["ConnectionString"].ToString());
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Clientid,C_Name,C_website,Status,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy from clientDetails where Clientid=@Clientid and C_Name=@C_Name";
                cmd.Parameters.AddWithValue("@C_Name", strCname);
                cmd.Parameters.AddWithValue("@Clientid", strCvalue);
                cmd.Connection = conn;
                conn.Open();

                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        company.Add(string.Format("{0}-{7}", sdr["Clientid"], sdr["C_Name"], sdr["C_website"], sdr["Status"], sdr["CreatedDate"], sdr["CreatedBy"], sdr["ModifiedDate"], sdr["ModifiedBy"]));
                    }
                }
                conn.Close();
                }



        }
        //else
        //{
        //    return strLvl.ToArray();
        //}

        return company.ToArray();
    }

and ajax method is here

 <script type="text/javascript">
    function OnTreeClick(evt) {
        var src = window.event != window.undefined ? window.event.srcElement : evt.target;
        var nodeClick = src.tagName.toLowerCase() == "a";

        if (nodeClick) {

            var nodeText = src.innerText || src.innerHTML;
            var nodeValue = GetNodeValue(src);

            var nodePath = src.href.substring(src.href.indexOf(",") + 2, src.href.length - 2);
            //alert(nodePath.toLowerCase());

                if (nodePath.indexOf("\\") > -1)
                {
                    var level = "2";
                  //  alert("Second level ");
                }
                else
                {
                    var level = "1";
                  //  alert(" first level ");
                }


            //var nodestat = 
            alert("Text: " + nodeText + "," + "Value: " + nodeValue );
            var cmpny = {};
            cmpny.Cname = nodeText;
            cmpny.Cvalue = nodeValue;
            cmpny.Clevel = level;
            $.ajax({
                type: "POST",
                url: "CS.aspx/Cdetails",
                data: '{cmpny: ' + JSON.stringify(cmpny) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    div_ClientLoc.innerHTML = response.d;//here how can i append those tag and div_ClientLoc is div id
                   // window.location.reload();
                }
            });
            return false;
        }
        return true;
    }
    function GetNodeValue(node) {
        var nodeValue = "";
        //alert(node.href.toLowerCase());
        var nodePath = node.href.substring(node.href.indexOf(",") + 2, node.href.length - 2);
       // alert(nodePath.toLowerCase());
        var nodeValues = nodePath.split("\\");
        if (nodeValues.length > 1)
            nodeValue = nodeValues[nodeValues.length - 1];
        else
            nodeValue = nodeValues[0].substr(1);

        return nodeValue;
    }

can anyone please explain me how can i solve this problem

  • 写回答

2条回答 默认 最新

  • weixin_33675507 2017-03-02 04:36
    关注

    You are overwriting the html of div_ClientLoc by assigning the result to innerHTML. You can use jQuery append() method to append the response result in existing html of div_ClientLoc.

    $("#div_ClientLoc").append(response.d);
    

    Or you can also append the result in div_ClientLoc.innerHTML by using Addition Assignment Operator += instead of assignment operator = like as under

    div_ClientLoc.innerHTML += response.d;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?