xmlrequest的ajax传递json序列化的数据

页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="productType.aspx.cs" Inherits="ProductManagement_productType" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>产品类别</title>

<script src="../js/jquery-1.4.3.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(
function(){
$(
"#<%=add.ClientID %>").click(function(){
$(
"#stypeadd").css({display:"block"});
$(
"#stypeadd :text").val("");
$(
"#<%=tj.ClientID %>").val("提交");
$(
"#<%=tjtext.ClientID %>").val("提交");
});
})
function checkForm(){
if(document.getElementById("<%=addName.ClientID %>").value==""||document.getElementById("<%=addDescript.ClientID %>").value=="")
{
alert(
"类别名称或类别描述不能为空!");
return false;
}
return true;
}
function CheckDel(obj){
var id=obj.parentNode.childNodes[0].value;
document.getElementById(
"<%=delid.ClientID %>").value=id;
if(confirm("确定删除?"))return true;
return false;
}
var xmlrequest;
xmlrequest
=false;
function InitObject(){
try {
xmlrequest
= new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlrequest
= new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlrequest
= new XMLHttpRequest();
}
}
}
function ShowAddmenu(id){
var currentId=id;
document.getElementById(
"stypeadd").style.display="block";
document.getElementById(
"<%=tj.ClientID %>").value="修改";
document.getElementById(
"<%=tjtext.ClientID %>").value="修改";
document.getElementById(
"<%=idtext.ClientID %>").value=currentId;
if(!xmlrequest)
InitObject();
if(xmlrequest){
xmlrequest.open(
"get","../ajax_post/ajax_productType.ashx?id="+encodeURI(currentId) + "&d=" +Math.random(),true);
xmlrequest.onreadystatechange
=UpdatePage;
xmlrequest.send(
null);
}
}
function UpdatePage(){
if(xmlrequest.readyState==4){
var response=xmlrequest.responseText;
if(response=="参数错误")
alert(
"参数错误");
else if(response=="发生异常,请刷新后重试")
alert(
"发生异常,请刷新后重试");
else{
var result=eval('('+response+')');
document.getElementById(
"addName").value=result.salesname;
document.getElementById(
"addDescript").value=result.salesdescript;
document.getElementById(
"addRemarks").value=result.remarks;
}
}
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
产品管理>>产品类别管理
</div>
<br />
<div>
类别名称:<input type="text" runat="server" id="txtName" />
<input type="button" runat="server" id="search" value="搜索" onserverclick="Search_Click" />
<input type="button" runat="server" id="add" value="添加" />
</div>
<div>
<table>
<tr><td>类别名称</td><td>描述</td><td>修改</td><td>删除</td></tr>
<%foreach (Models.salestype st in lsts)
{
%><tr><td><%=st.salesname %></td><td><%=st.salesdescript %></td><td><a href='javascript:ShowAddmenu(<%=st.id %>)'>修改</a></td>
<td><input type="hidden" id="rowid" value="<%=st.id %>" /><asp:LinkButton ID="Delete" runat="server" OnClick="Delete_Click" OnClientClick="return CheckDel(this)">删除</asp:LinkButton></td></tr><% } %>
</table>
<input type="hidden" id="delid" runat="server" value="" />
</div>
<div id="stypeadd" style="display: none;">
<table><tr><td>添加/修改产品类别</td></tr><tr><td>类别名称:</td><td><input type="text" id="addName" runat="server" /></td></tr>
<tr><td>类别描述:</td><td><input type="text" id="addDescript" runat="server" /></td></tr>
<tr><td>备注:</td><td><input type="text" id="addRemarks" runat="server" /></td></tr>
<tr><td><input type="hidden" id="tjtext" runat="server" value="" /><input type="hidden" id="idtext" runat="server" value="" />
<asp:Button ID="tj" runat="server" OnClick="tc_click" OnClientClick="return checkForm()" />&nbsp;&nbsp;&nbsp;
<input type="reset" value="取消" onclick='document.getElementById("stypeadd").style.display="none"' /></td></tr>
</table>
</div>
</form>
</body>
</html>


 

cs代码:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Web.UI.WebControls;
4 using DAL;
5 using Models;
6
7 public partial class ProductManagement_productType : System.Web.UI.Page
8 {
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 bindData();
12
13 }
14 public List<salestype> lsts = new List<salestype>();
15 private void bindData()
16 {
17 lsts = SalestypeServices.GetAll();
18 }
19 protected void Search_Click(object sender, EventArgs e)
20 {
21 string name = txtName.Value;
22 lsts = SalestypeServices.GetByName(name);
23 }
24 protected void tc_click(object sender, EventArgs e)
25 {
26 string stid = idtext.Value;
27 string stName = addName.Value;
28 string stDesc = addDescript.Value;
29 string stRemark = addRemarks.Value;
30 if (stName == "" || stDesc == "")
31 {
32 RegisterClientScriptBlock("aa", "<script>alert('类别名称或类别描述不能为空!')</script>");
33 return;
34 }
35 if (tjtext.Value == "提交")
36 {
37 salestype st = new salestype();
38 st.salesname = stName;
39 st.salesdescript = stDesc;
40 st.remarks = stRemark;
41 if (SalestypeServices.AddSalesType(st))
42 RegisterClientScriptBlock("aa", "<script>alert('添加成功!')</script>");
43 else RegisterClientScriptBlock("aa", "<script>alert('添加失败!')</script>");
44 bindData();
45 return;
46 }
47 else if (tjtext.Value == "修改")
48 {
49 salestype st = new salestype();
50 st.id = Int32.Parse(stid);
51 st.salesname = stName;
52 st.salesdescript = stDesc;
53 st.remarks = stRemark;
54 if (SalestypeServices.UpdateSalesType(st))
55 RegisterClientScriptBlock("aa", "<script>alert('修改成功!')</script>");
56 else RegisterClientScriptBlock("aa", "<script>alert('修改失败!')</script>");
57 bindData();
58 return;
59 }
60 }
61
62 protected void Delete_Click(object sender, EventArgs e)
63 {
64 LinkButton deleteBtn = (LinkButton)sender;
65 string id = delid.Value;
66 try
67 {
68 if (SalestypeServices.DeleteSalesType(id))
69 RegisterClientScriptBlock("aa", "<script>alert('删除成功!')</script>");
70 else RegisterClientScriptBlock("aa", "<script>alert('删除失败!')</script>");
71 }
72 catch (Exception)
73 {
74 RegisterClientScriptBlock("aa", "<script>alert('删除失败!')</script>");
75 }
76 finally { bindData(); }
77 }
78 }


ajax服务器:

View Code
 1 <%@ WebHandler Language="C#" Class="Handler" %>
2
3 using System;
4 using System.Web;
5 using DAL;
6 using Models;
7
8 public class Handler : IHttpHandler
9 {
10
11 public void ProcessRequest(HttpContext context)
12 {
13 context.Response.ContentType = "text/plain";
14 if (context.Request.QueryString["id"] != null && context.Request.QueryString["id"].ToString() != "")
15 {
16 try
17 {
18 string id = context.Request.QueryString["id"].ToString();
19 salestype st = new salestype();
20 st = SalestypeServices.GetById(id);
21 string jsonString = JsonHelper.JsonSerializer<salestype>(st);
22 context.Response.Write(jsonString);
23 }
24 catch
25 {
26 context.Response.Write("发生异常,请刷新后重试");
27 }
28 }
29 else
30 context.Response.Write("参数错误");
31 }
32
33 public bool IsReusable
34 {
35 get
36 {
37 return false;
38 }
39 }
40
41 }


jsonHelper:

View Code
 1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 using System.Runtime.Serialization.Json;
13 using System.IO;
14 using System.Text;
15
16 /// <summary>
17 ///JsonHelper 的摘要说明
18 /// </summary>
19 public class JsonHelper
20 {
21 public static string JsonSerializer<T>(T t)
22 {
23 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
24 MemoryStream ms = new MemoryStream();
25 ser.WriteObject(ms, t);
26 string jsonString = Encoding.UTF8.GetString(ms.ToArray());
27 ms.Close();
28 return jsonString;
29 }
30
31 public static T JsonDeserializer<T>(string jsonString)
32 {
33 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
34 MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
35 T obj = (T)ser.ReadObject(ms);
36 return obj;
37 }
38
39 }

 

 

 

 

posted @ 2012-02-17 20:16  捂汗  阅读(834)  评论(4编辑  收藏  举报