如何从webmethod到jquery获取数组值以供显示? [英] How to get Array values from webmethod to jquery for display?

查看:57
本文介绍了如何从webmethod到jquery获取数组值以供显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一段时间间隔内显示来自数据库的数据,所以我使用了Timer控件,但是在每次打勾时,div(聊天框)最小化,所以我想避免在每个帖子上最小化我使用Jquery来webmethode概念如下。



调用C#数组类型webmethod。



 <   script     type   =  text / javascript >  
$(document).ready(function(){
$(#tblCustomers tbody tr)。remove();
$。 ajax({
类型:POST,
url:GetDataByJquery.aspx / GetMessages,
data:'{roomId:'+ $([id $ = lblRoomId] ).html()+'}',
contentType:application / json; charset = utf-8,
dataType:json,
成功:函数(数据){
响应($ .map(data.d,function(item){
var rows =< tr >
+ < td class =' customertd' > + item.Username +< / td >
+< td class =' customertd' > + item.Sex +< / td >
+< td class =' customertd' > + item.Text +< / td >
+< td class =' customertd' < span class =code-keyword>> + item.TimeStamp +& / td >
+< td class =' customertd' > + item.UserID +< / td >
+< / tr > ;
$('#tblCustomers tbody')。append(rows);
}))
},
失败:函数(响应){
alert(response.d);
}
});
});
< / script >





从sqlserver获取数据并在数组中重新导航。



 public static Messages [] GetMessages(string roomId)
{
List< Messages> messages = new List< Messages>();
string strConnString = ConfigurationManager.ConnectionStrings [LinqChatConnectionString]。ConnectionString;

使用(SqlConnection con = new SqlConnection(strConnString))
{
using(SqlDataAdapter sda = new SqlDataAdapter())
{
string query = [Get_Messages];
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ roomId,roomId);
cmd.Connection = con;
sda.SelectCommand = cmd;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
Messages message = new Messages();
message.Username = reader.GetString(0);
message.Sex = reader.GetString(1);
message.Text = reader.GetString(2);
message.TimeStamp = reader.GetDateTime(3);
message.UserID = reader.GetInt32(4);
messages.Add(message);
}
}
}
返回messages.ToArray();
}





但是我无法显示数据..那么如何显示它?

解决方案

(document).ready(function(){


(#tblCustomers tbody tr)。remove();


.ajax({
type:POST,
url:GetDataByJquery.aspx / GetMessages,
data:'{roomId:' +

I want to display the data from database on some time interval so I used Timer control, but on every tick fire the div (chat box) minimizing, so I want to avoid this minimizing on every post back I used Jquery to webmethode concept like below.

to call C# array type webmethod.

<script type="text/javascript">
        $(document).ready(function () {
            $("#tblCustomers tbody tr").remove();
            $.ajax({
                type: "POST",
                url: "GetDataByJquery.aspx/GetMessages",
                data: '{roomId: "' + $("[id$=lblRoomId]").html() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        var rows = "<tr>"
                    + "<td class='customertd'>" + item.Username + "</td>"
                    + "<td class='customertd'>" + item.Sex + "</td>"
                    + "<td class='customertd'>" + item.Text + "</td>"
                    + "<td class='customertd'>" + item.TimeStamp + "</td>"
                    + "<td class='customertd'>" + item.UserID + "</td>"
                    + "</tr>";
                        $('#tblCustomers tbody').append(rows);
                    }))
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        });
    </script>



Got data from sqlserver and reterning in array.

public static Messages[] GetMessages(string roomId)
   {
       List<Messages> messages = new List<Messages>();
       string strConnString = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;

       using (SqlConnection con = new SqlConnection(strConnString))
       {
           using (SqlDataAdapter sda = new SqlDataAdapter())
           {
               string query = "[Get_Messages]";
               SqlCommand cmd = new SqlCommand(query);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@roomId", roomId);
               cmd.Connection = con;
               sda.SelectCommand = cmd;
               con.Open();
               SqlDataReader reader = cmd.ExecuteReader();

               while (reader.Read())
               {
                   Messages message = new Messages();
                   message.Username = reader.GetString(0);
                   message.Sex = reader.GetString(1);
                   message.Text = reader.GetString(2);
                   message.TimeStamp = reader.GetDateTime(3);
                   message.UserID = reader.GetInt32(4);
                   messages.Add(message);
               }
           }
       }
       return messages.ToArray();
   }



but I can't display the data..so how to display it?

解决方案

(document).ready(function () {


("#tblCustomers tbody tr").remove();


.ajax({ type: "POST", url: "GetDataByJquery.aspx/GetMessages", data: '{roomId: "' +


这篇关于如何从webmethod到jquery获取数组值以供显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆