当前位置: 代码迷 >> ASP.NET >> asp.net中使用.ajax传参,从后台获得list数据,怎么显示
  详细解决方案

asp.net中使用.ajax传参,从后台获得list数据,怎么显示

热度:8667   发布时间:2013-02-25 00:00:00.0
asp.net中使用.ajax传参,从后台获得list数据,如何显示
asp.net中使用.ajax传参,从后台获得list数据,如何显示在前台?
看到很多说转化为json数据之后再回传。这个后台代码该如何书写呢?
接触asp.net才两个月,.ajax这种以前只在java中使用过。不过,貌似,还是有所区别的。
求解答啊。倒腾两天了,没结果。愁。
求代码,求代码啊。
非常感谢各位。


代码运行平台: VS2010,.net 4以上

前台代码:
C# code
<%@ Page Title="" Language="C#" MasterPageFile="~/Master/student.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="user_test2" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"><script type="text/javascript" src="../js/jquery-1.7.js"></script><script type="text/javascript">     $(function () {         $("#<%=ceshi.ClientID %>").click(function () {                       var bianliang = "3";           alert(“****”);//显示正常            $.ajax({                type: "post",                url: "test2.aspx/getschoolajax",                contentType: "application/json; charset=utf-8",                dataType: "json",                data: "citynum=" + bianliang,                beforeSend: function (XMLHttpRequest) {                    $("#schools").text("正在查询...");                                  },                success: function Msg(data) {                    $("#schools").css("color", "red");                    $("#schools")("-----data------" + data);                                   },                              error: function () {                    alert("error");//得不到数据,一直提示error                }            });        });</script></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><div> <asp:Button ID="ceshi" runat="server" Text="Button"  />  <div id="schools"  class="bg1">    <!-- 此处显示学校列表 -->                                   </div><div></asp:Content>



后台cs代码
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using PBLL;using PModel;using System.Data;public partial class user_test2 : System.Web.UI.Page{    userinfoManager userinfo = new userinfoManager();       protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {                           }    }  protected List<string> getschoolajax()    {              int citynumber = Convert.ToInt32(Request.QueryString["citynum"]);        List<string> schoollist = userinfo.getschoolbycitynumberlist(citynumber);        if (schoollist.Count != 0)        {                       return schoollist;                   }        else        {                      return null;        }    }}


------解决方案--------------------------------------------------------
C# code
  /// <summary>        /// Serialize object to string of json format        /// </summary>        private string JsonSerializer<T>(T t)        {            var ser = new DataContractJsonSerializer(typeof(T));            var ms = new MemoryStream();            ser.WriteObject(ms, t);            string jsonString = Encoding.UTF8.GetString(ms.ToArray());            ms.Close();            return jsonString;        }        /// <summary>        /// Deserialize string of json format to object        /// </summary>        private T JsonDeserialize<T>(string jsonString)        {            var ser = new DataContractJsonSerializer(typeof(T));            var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));            var obj = (T)ser.ReadObject(ms);            return obj;        }
  相关解决方案