使用ajax

时间:2015-12-28 15:42:38

标签: javascript html asp.net ajax

我被困在我网站的这一部分,因为我的控制器上没有应用外部javascript

我在这里做的是我正在使用ajax从WebMethod GetSection 获取数据,因为这个数据还没有在第一次加载页面时所以javascript没有适用于它

  

HTML:

<a id="phone_number" href="#">Your Phone Number</a>
//the button i press to active GetSection function
  

和java脚本方:

   $(document).on('click', '#phone_number', function (e) {
                    e.preventDefault();
    //this way doesn't help me 
//////////////////////////////////////
                    var script1 = document.createElement("script");
                    script1.src = "js/jquery.crs.min.js";
                    document.getElementsByTagName("head")[0].appendChild(script1);

                    script1 = document.createElement("script");
                    script1.src = "js/bootstrap-formhelpers.js";
                    document.getElementsByTagName("head")[0].appendChild(script1);

                    script1 = document.createElement("script");
                    script1.src = "js/bootstrap-formhelpers-phone.js";
                    document.getElementsByTagName("head")[0].appendChild(script1);

                    script1 = document.createElement("script");
                    script1.src = "js/bootstrap-formhelpers-countries.js";
                    document.getElementsByTagName("head")[0].appendChild(script1);
//////////////////////////////////////
                    document.getElementById('content_area').innerHTML = Get_Data();

                });
  

GetData函数:

 function Get_Data() {
            var returned;
            //send a query to server side to present new content
            var par = { section: data };
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetSection",
                data: "{}",
                contentType: "application/json;",
                dataType: "json",
                async: false,
                success: function (data) {
                    returned = data.d;
                },
                error: function (data) {
                    returned = data.d;
                }
            });
            return returned;
        };

    </script>
  

我的网络方法部分:

StringBuilder sb = new StringBuilder();
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
            {
                con.Open();
string select = "select p.phone phone  from  person p where p.id=@ID";

                using (SqlCommand sel = new SqlCommand(select, con))
                {
                    sel.Parameters.AddWithValue("ID", pid);

                    using (var reader = sel.ExecuteReader())
                    {
                        reader.Read();
                        sb.AppendLine("<div class='container-fluid'>");
                        sb.AppendLine("<div class='form-group'>");
                        sb.AppendLine("<label class='control-label'>Your Country</label><select id ='countries_phone' style ='color: black; width: 100%; background-color: whitesmoke' class='form-control bfh-countries' data-country='SY'></select>");
                        sb.AppendLine("<label class='control-label'>Phone Number</label><input class='form-control' value='" + reader["phone"].ToString() + "' type='text' readonly id='phone'/>");
                        sb.AppendLine("<button type = 'button' class='pull-right btn-link btn btn-xs' id='ephone'>Edit</button>");
                        sb.AppendLine("<br/>");
                    }
                }
}

    return sb.ToString();

0 个答案:

没有答案