评论

收藏

[jQuery] jquery $.getJSON 在asp.net中的用法例子

开发技术 开发技术 发布于:2021-07-31 10:35 | 阅读数:249 | 评论:0

下面是asp.net页面
test.aspx
<script type="text/jscript" src="../js/jquery-

1.2.5.js"></script>
  <script type="text/javascript">
    //获取对象列表
    $(function(){
      $.getJSON("Handler.ashx",function(json){
      $.each(json.data,function(i)

{$('#content').append("姓名:"+json.data[i].name+"----性

别:"+json.data[i].avg+"----时间:"+json.data[i].year

+"<br/>");});
      });
    });
  </script>
 <script type="text/jscript" src="../js/jquery-

1.2.5.js"></script>
<script type="text/javascript">
//获取一个对象
   function checkServer() {
     $.getJSON("AjaxGetService.aspx?planNo=" + 

$("#txtPlanNo").val(), function(data) {

//       alert(data.toString());
//       alert(data.IsTrustee);
       if (data.IsTrustee==true) {
         $("#cheTrustee").attr("checked", true);
       }
       else {
         $("#cheTrustee").attr("checked",'');
       }
       if (data.IsAccount == true) {
         $("#cheAccount").attr("checked", true);
       }
       else {
         $("#cheAccount").attr("checked", '');
       }
       //       alert(data[0].CityName); ,

{planNo:$("#txtPlanNo").val()}
     });
    
  }

</script>
 
<body>
  <form id="form1" runat="server">
  <div id="content"></div>
  <div id="table">
   <table width="90%" border="0" align="center" 

cellpadding="3" cellspacing="0" class="wen">
                <tr>
                  <td height="10" 

colspan="3" align="left"></td>
                  </tr>
                <tr>
                  <td height="32" 

align="left">计划登记号</td>
                  <td width="68%">
                  <input runat=server 

type="text" name="textfield2" id="txtPlanNo" 
                      

class="inputstyle"  onblur="checkServer();" 

maxlength="12"/>
                   
                  </td>
                  <td width="6%"><%--

onmousemove="MM_showHideLayers('div1','','show')" 

onmouseout="MM_showHideLayers('div1','','hide')"--%>
                    </td>
                </tr>
                <tr>
                  <td height="32" 

align="left"> </td>
                  <td class="greenwen">
                    <input runat=server 

type="checkbox" name="checkbox"  onclick="return false;"
                      id="cheTrustee" 

readonly="readonly" />受托服务
                    <input  

runat=server type="checkbox" name="checkbox2" 

id="cheAccount" onclick="return false;"
                      

readonly="readonly" />账管服务</td>
                  <td> </td>
                </tr>
                <tr>
                  <td height="32" 

align="left" title="所属企业">所属企业</td>
                  <td><input runat=server 

type="text" name="textfield2" id="txtEnterpriceManger" 
                      

class="inputstyle" /></td>
                  <td> </td>
                </tr>
                <tr>
                  <td height="32" 

align="left">账号选择</td>
                  <td>
                    <asp:RadioButton 

ID="radReg" runat="server" GroupName="selectAccount" />
                    为我分配账号

<asp:RadioButton ID="radAccount" runat="server" 
                      

GroupName="selectAccount" />
                    输入账号</td>
                  <td> </td>
                </tr>
                <tr>
                  <td height="32" 

align="left" class="greenwen">请输入账号</td>
                  <td><input  

runat=server type="text" name="textfield3" 

id="txtAccount" 
                      

class="inputstyle"/></td>
                  <td> </td>
                </tr>
                <tr>
                  <td 

align="left"> </td>
                  <td height="40">
                   <asp:Button ID="btnOK" 

runat="server" CssClass="tijiaobutton" 
                      

onclick="btnOK_Click" />
                 </td>
                  <td> </td>
                </tr>
                </table>
</div>
  </form>
</body>

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Text;
public class Handler : IHttpHandler {
  
  public void Proce***equest (HttpContext context) {
    /*context.Response.ContentType = "text/plain";
    string data = "[{name:"fan",age:26},{name:"wang",age:25}]";//构建的json数据
    context.Response.Write(data);*/

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("name", typeof(string));
    dt.Columns.Add("year", typeof(string));
    dt.Columns.Add("avg", typeof(string));
    DataRow tr = dt.NewRow();
    tr["name"] = "shshshdy";
    tr["year"] = "2005-02-02";
    tr["avg"] = "男";
    dt.Rows.Add(tr);
    //ds.Tables.Add(dt);
    DataRow tr2 = dt.NewRow();
    tr2["name"] = "xiaoming";
    tr2["year"] = "2007-02-02";
    tr2["avg"] = "女";
    dt.Rows.Add(tr2);
    ds.Tables.Add(dt);



    context.Response.Clear();
    context.Response.ContentEncoding = Encoding.UTF8;
    context.Response.ContentType = "application/json";
    //Response.Write("{"success":true,"name":"shshshdy","year":"2009-12-12"}");
    context.Response.Write(getJSON(ds));
    context.Response.Flush();
    context.Response.End();
  }
 
  public bool IsReusable {
    get {
      return false;
    }
  }
  private static string getJSON(DataSet ds)//这里调用了JSON帮助文件,jsonHelp.cs 就不放出源码了
  {
    Json.JSONHelper jsonHelp = new Json.JSONHelper();

    jsonHelp.success = true;
    //jsonHelp.totlalCount = getPrjListCount();
    jsonHelp.totlalCount = ds.Tables[0].Rows.Count;

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
      jsonHelp.AddItem("name", dr["name"].ToString());
      jsonHelp.AddItem("avg", dr["avg"].ToString());

      if (dr["year"] != DBNull.Value)
      {
        jsonHelp.AddItem("year", Convert.ToDateTime(dr["year"]).ToString("yyyy/MM/dd"));
      }
      else
      {
        jsonHelp.AddItem("year", string.Empty);
      }

      jsonHelp.ItemOk();
    }

    string strResult = jsonHelp.ToString();
    return strResult;
  }
}
 

在asp.net页面中page_load方法中

 

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=代码
protected void Page_Load(object sender, EventArgs e)
    {
      string planNo = string.Empty;
      if (Request.QueryString["planNo"] != null)
      {

         planNo = Request.QueryString

["planNo"].ToString();
      }
    if (!string.IsNullOrEmpty(planNo))
    {
//从数据库中取到OpenServiceInfo 对象
      //BLL.OpenSevice bll = new 

Sysnet.EDP.EAF.F3.BLL.OpenSevice();
      //OpenServiceInfo info = 

bll.GetOpenServiceByPlanNo(planNo);
   OpenServiceInfo info =new OpenServiceInfo();
   info.IsAccount =true;
info.IsTrustee = false;
info.LoginName ="张三";

//序列化对象
      JavaScriptSerializer jss = new 

JavaScriptSerializer();
      string strJson =jss.Serialize(info);

      Response.Clear();
      Response.ContentEncoding = Encoding.UTF8;
      Response.ContentType = "application/json";
      Response.Write(strJson);
      Response.Flush();
      Response.End();
    }
     
    }

  public class OpenServiceInfo
  {
    private string _ID;
    private string _PlanNo;
    private bool _IsTrustee=false;
    private bool _IsAccount = false;
    private bool _IsInvestment = false;
    private string _EnterpriseManagement;
    private string _LoginName;
    private string _Remark;
    /// <summary>
    /// ID
    /// </summary>
    public string ID
    {
      get
      {
        return _ID;
      }
      set
      {
        _ID = value;
      }
    }
    /// <summary>
    /// 计划登记号
    /// </summary>
     public string PlanNo
    {
      get
      {
        return _PlanNo;
      }
      set
      {
        _PlanNo = value;
      }
    }
     /// <summary>
     /// 受托服务
     /// </summary>
     public bool  IsTrustee
     {
       get
       {
         return _IsTrustee;
       }
       set
       {
         _IsTrustee = value;
       }
     }
     /// <summary>
     /// 账管服务
     /// </summary>
     public bool IsAccount
     {
       get
       {
         return _IsAccount;
       }
       set
       {
         _IsAccount = value;
       }
     }
     /// <summary>
     /// 投资服务
     /// </summary>
     public bool IsInvestment
     {
       get
       {
         return _IsInvestment;
       }
       set
       {
         _IsInvestment = value;
       }
     }
     /// <summary>
     /// 可管理的企业
     /// </summary>
     public string EnterpriseManagement
     {
       get
       {
         return _EnterpriseManagement;
       }
       set
       {
         _EnterpriseManagement = value;
       }
     }
     /// <summary>
     /// 登陆名-账号
     /// </summary>
     public string LoginName
     {
       get
       {
         return _LoginName;
       }
       set
       {
         _LoginName = value;
       }
     }
     /// <summary>
     /// 备注
     /// </summary>
     public string Remark
     {
       get
       {
         return _Remark;
       }
       set
       {
         _Remark = value;
       }
     }
  }



关注下面的标签,发现更多相似文章