ArrayList和的WebMethod [英] Arraylist and webmethod

查看:138
本文介绍了ArrayList和的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在ASP.net Web方法,它的输出是城市的一个ArrayList是从SQL Server数据库中读取。
这是的WebMethod在客户方使用Jquery调用。
但我不知道如何使用jquery来读取数组列表中的每个项目。例如每个城市和它的等效ID。
下面是我的WebMethod:

I have written a web method in ASP.net which it's output is an ArrayList of cities that is read from Sql server database. this webmethod is called using Jquery in clientside. but I don't know how to read each item of array list using jquery. for example every city and it's id equivalent. Below is my Webmethod:

public ArrayList showcity(int s)
    {
            ArrayList list = new ArrayList();
            String strConnString = ConfigurationManager
                .ConnectionStrings["ConnectionCS"].ConnectionString;
            String strQuery = "select ID, City from tbl_city where stateid=@s";
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@s", s);
                    cmd.CommandText = strQuery;
                    cmd.Connection = con;
                    con.Open();
                    SqlDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                    {
                        list.Add(new ListItem(
                       sdr["City"].ToString(),
                       sdr["ID"].ToString()
                        ));
                    }
                    con.Close();
                   return list;
                }
            }

这是我的客户方code:

and this is my clientside code:

function showcity() {
        $.ajax(
    { url: "../AjaxServices/StateCity.asmx/showcity",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        data: '{s: ' + $('#<%=DpState.ClientID%>').val() + '}',
        success: function(data) {
             ***// what should I write here to access every item separately*** 
          },
        error: function() { alert("Error"); }
    })
}

如果我用警报(data.d)我会得到[对象] [对象] [对象] [对象],.....

If I use alert(data.d) I will get [object][object][object][object],.....

推荐答案

我找到了一个解决方案,我自己,所以我在这里分享它,只是要小心文本他们是区分大小写

I found a solution myself so I share it here, just be careful about Value and Text they're case sensitive

 success: function(data) {
                $.each(data.d, function() {
                    alert(this['Value'] + ':' + this['Text']);
                })
            }

这篇关于ArrayList和的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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