C#如何从数组类型Webmethode访问Asp.net标签值? [英] C# How to Access Asp.net Label value from array type Webmethode?

查看:54
本文介绍了C#如何从数组类型Webmethode访问Asp.net标签值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从jquery调用数组类型的方法,所以下面的代码工作正常。



 <   script     type   =  text / javascript >  
$(document).ready(function(){
$(#tblCustomers tbody tr)。remove();
$ .ajax({
type :POST,
url:GetDataByJquery.aspx / GetMessages,
data:'{roomId:'+ $(#lblRoomId)。val()+'}',
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);
}))
},
失败:函数(响应){
alert(response.d);
}
});
});
< / script >







因此,我无法通过webmthode获取标签控制值。



 [WebMethod] 
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.GetString(3);
message.UserID = reader.GetString(4);
messages.Add(message);
}
}
}
返回messages.ToArray();
}





那么如何将asp.net标签控制值传递给'GetMessages'方法的参数?

解决方案

(document).ready(function(){


(#tblCustomers tbody tr)。remove();


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

I want to call array type methode from jquery so below code is working fine.

<script type="text/javascript">
        $(document).ready(function () {
            $("#tblCustomers tbody tr").remove();
            $.ajax({
                type: "POST",
                url: "GetDataByJquery.aspx/GetMessages",
                data: '{roomId: "' + $("#lblRoomId").val() + '" }',
                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>




so in this I cant get label control value to pass below webmthode.

[WebMethod]
        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.GetString(3);
                        message.UserID = reader.GetString(4);
                        messages.Add(message);
                    }
                }
            }
            return messages.ToArray();
        }



So how to pass asp.net label control value to 'GetMessages' methode's parameter?

解决方案

(document).ready(function () {


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


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


这篇关于C#如何从数组类型Webmethode访问Asp.net标签值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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