CSharp: get data from WebMethod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | <%@ Page Language= "C#" AutoEventWireup= "true" CodeBehind= "WebMethodAjaxdemo.aspx.cs" Inherits= "DuFullCalendar.WebMethodAjaxdemo" %> <!doctype html> <html> <head id= "Head1" runat= "server" > <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge,chrome=1" > <meta name= "viewport" content= "width=device-width, initial-scale=1, shrink-to-fit=no" > <title>geovindu</title> <meta name= "Description" content= "geovindu" > <meta name= "Keywords" content= "geovindu" > <meta name= "author" content= "geovindu" /> <style type= "text/css" > .tblCustomers { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } .customerth { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } .customertd { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; } </style> <script src= "jquery/jquery-3.2.1.min.js" type= "text/javascript" ></script> <script type= "text/javascript" > $(document).ready(function () { $( "#ddlCountry" ).change(function () { $( "#tblCustomers tbody tr" ).remove(); $.ajax({ type: "POST" , url: "WebMethodAjaxdemo.aspx/GetCustomers" , data: '{country: "' + $( this ).val() + '" }' , contentType: "application/json; charset=utf-8" , dataType: "json" , success: function (data) { console.log(data.d); // var dudata = jQuery.parseJSON(data.d); //console.log(dudata); $.each(data.d, function (i, item) { //console.log(item.CustomerID); //console.log(item.CompanyName); var rows = "<tr>" + "<td class='customertd'>" + item.CustomerID + "</td>" + "<td class='customertd'>" + item.CompanyName + "</td>" + "<td class='customertd'>" + item.ContactName + "</td>" + "<td class='customertd'>" + item.ContactTitle + "</td>" + "<td class='customertd'>" + item.City + "</td>" + "<td class='customertd'>" + item.Phone + "</td>" + "</tr>" ; $( '#tblCustomers tbody' ).append(rows); }); }, failure: function (response) { var r = jQuery.parseJSON(response.responseText); alert( "Message: " + r.Message); alert( "StackTrace: " + r.StackTrace); alert( "ExceptionType: " + r.ExceptionType); } }); }); }); </script> </head> <body> <form id= "form1" runat= "server" > <div> 国家: <asp:DropDownList ID= "ddlCountry" runat= "server" > <asp:ListItem Text= "Brazil" Value= "Brazil" ></asp:ListItem> <asp:ListItem Text= "France" Value= "France" ></asp:ListItem> <asp:ListItem Text= "Germany" Value= "Germany" ></asp:ListItem> <asp:ListItem Text= "Spain" Value= "Spain" ></asp:ListItem> <asp:ListItem Text= "USA" Value= "USA" ></asp:ListItem> <asp:ListItem Text= "CN" Value= "CN" ></asp:ListItem> <asp:ListItem Text= "Mexico" Value= "Mexico" ></asp:ListItem> </asp:DropDownList> </div> <br /> <div> <table id= "tblCustomers" class = "tblCustomers" > <thead> <tr> <th align= "left" class = "customerth" >客户ID</th> <th align= "left" class = "customerth" >公司名称</th> <th align= "left" class = "customerth" >联系人</th> <th align= "left" class = "customerth" >称谓</th> <th align= "left" class = "customerth" >城市</th> <th align= "left" class = "customerth" >电话</th> </tr> </thead> <tbody> </tbody> </table> </div> </form> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Web.Services; namespace DuFullCalendar { /// <summary> /// geovindu,Geovin Du,涂聚文 /// /// </summary> public partial class WebMethodAjaxdemo : System.Web.UI.Page { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="country"></param> /// <returns></returns> [System.Web.Services.WebMethod] public static Customer[] GetCustomers( string country) { List<Customer> customers = new List<Customer>(); //string query = string.Format("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle]," + // "[City], [Phone] FROM [Customers] WHERE Country LIKE '%{0}%'", country); //using (SqlConnection con = // new SqlConnection("your connection string")) //{ // using (SqlCommand cmd = new SqlCommand(query, con)) // { // con.Open(); // SqlDataReader reader = cmd.ExecuteReader(); // while (reader.Read()) // { // Customer customer = new Customer(); // customer.CustomerID = reader.GetString(0); // customer.CompanyName = reader.GetString(1); // customer.ContactName = reader.GetString(2); // customer.ContactTitle = reader.GetString(3); // customer.City = reader.GetString(4); // customer.Phone = reader.GetString(5); // customers.Add(customer); // } // } //} Customer customer = new Customer(); customer.CustomerID = "1" ; customer.CompanyName = "深圳六福珠宝" ; customer.ContactName = "涂聚文" ; customer.City = "深圳市" ; customer.ContactTitle = "天下为公" ; customer.Phone = "138" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "2" ; customer.CompanyName = "广州六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "广州" ; customer.ContactTitle = "和而不同" ; customer.Phone = "139" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "3" ; customer.CompanyName = "武汉六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "武汉" ; customer.ContactTitle = "和而不同" ; customer.Phone = "137" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "4" ; customer.CompanyName = "北京六福珠宝" ; customer.ContactName = "Geovin Du" ; customer.City = "北京" ; customer.ContactTitle = "和而不同" ; customer.Phone = "136" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "5" ; customer.CompanyName = "上海六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "上海" ; customer.ContactTitle = "和而不同" ; customer.Phone = "131" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "5" ; customer.CompanyName = "重庆六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "重庆" ; customer.ContactTitle = "和而不同" ; customer.Phone = "132" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "5" ; customer.CompanyName = "西安六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "西安" ; customer.ContactTitle = "和而不同" ; customer.Phone = "133" ; customer.Country = "CN" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "6" ; customer.CompanyName = "纽约六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "纽约" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "USA" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "7" ; customer.CompanyName = "墨西哥六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "墨西哥" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "Mexico" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "8" ; customer.CompanyName = "墨尔本六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "墨尔本" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "Spain" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "9" ; customer.CompanyName = "圣保罗六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "圣保罗" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "Brazil" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "10" ; customer.CompanyName = "巴黎六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "巴黎" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "France" ; customers.Add(customer); customer = new Customer(); customer.CustomerID = "11" ; customer.CompanyName = "墨尔本六福珠宝" ; customer.ContactName = "gevoindu" ; customer.City = "墨尔本" ; customer.ContactTitle = "和而不同" ; customer.Phone = "1" ; customer.Country = "Germany" ; customers.Add(customer); // var selectCustomers = customers.Where(c => c.Country == country); return selectCustomers.ToArray(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace DuFullCalendar { /// <summary> /// 客户对象 /// 涂聚文 geovindu,Geovin Du /// </summary> public class Customer { /// <summary> /// 客户ID /// </summary> public string CustomerID { get ; set ; } /// <summary> /// 公司名称 /// </summary> public string CompanyName { get ; set ; } /// <summary> /// 联系人 /// </summary> public string ContactName { get ; set ; } /// <summary> /// 联联人称谓 /// </summary> public string ContactTitle { get ; set ; } /// <summary> /// 城市 /// </summary> public string City { get ; set ; } /// <summary> /// 电话号码 /// </summary> public string Phone { get ; set ; } /// <summary> /// 国家 /// </summary> public string Country { get ; set ; } } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
标签:
WebMethod
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2012-10-31 sql distinct