本文主要實現無動態刷新查詢後臺數據功能,主要用到ajax+ashx+sqlserver進行交互. 首先需要引用Jquery: html腳本: 前臺通過一個事件來調用ashx: 後臺來接收前臺傳過來的值,對其進行操作: SerializerHelper類的定義: 如果向後臺傳入多個參數在data裡面用 ...
本文主要實現無動態刷新查詢後臺數據功能,主要用到ajax+ashx+sqlserver進行交互.
首先需要引用Jquery:
<script language="javascript" type="text/javascript" src="../js/jquery.js">
html腳本:
<asp:TextBox ID="tb_corpName" runat="server" MaxLength="100" Width="369px"></asp:TextBox>
前臺通過一個事件來調用ashx:
<script type="text/javascript"> $(function () { $("#tb_corpName").blur(function () { //滑鼠失去焦點事件 var corpName = $("#tb_corpName").val(); $.ajax({ type: "post", //提交方式 url: "/ashx/FZGpyShowData.ashx", //一般處理程式的路徑 data: { corpName: corpName }, //向後臺傳入的值 dataType: "json", //返回值格式 success: function (data) { //返回成功後將要做的事,這裡是返回一個表 $("#tb_CreditCode").val(data[0].CreditCode); $("#tb_corpCode").val(data[0].CorpCode); $("#tb_linkTel").val(data[0].LinkPhone); $("#corpProvince option[text='" + data[0].StateName + "']").attr("selected", true); showCity(); $("#corpCity option[text='" + data[0].AdminAreaName + "']").attr("selected", true); $("#tb_address").val(data[0].Address); $("#tb_linkMan").val(data[0].LinkMan); $("#tb_Mobile").val(data[0].LinkMobile); } }); }) }) </script>
後臺來接收前臺傳過來的值,對其進行操作:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string corpName = context.Request["corpName"].Trim().ToString(); //接收前臺傳過來的參數 if (corpName != "") { string sql = @"select top 1 CorpName,CreditCode,CorpCode,LinkPhone,StateName,AdminAreaName,LinkMan,LinkMobile,Address from tbProductOrder as a inner join tbStateDic as b on a.StateNum = b.StateNum inner join tbAdminAreaClass on a.CityNum = AdminAreaClassID where CorpName =@CorpName order by CorpName"; SqlParameter[] par = new SqlParameter[1]; par[0] = new SqlParameter("@CorpName", corpName); DataSet ds = DBHerpler.Load(sql, par); string json = SerializerHelper.ToJsonString(ds.Tables[0]); //返回json類型的數據 context.Response.Write(json); context.Response.End(); } }
SerializerHelper類的定義:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.IO; using System.Xml.Serialization; using Newtonsoft.Json; /// <summary> /// SerializerHelper 的摘要說明 /// </summary> public static class SerializerHelper { /// <summary> /// 反序列化XML文件 /// </summary> public static T LoadFromXmlFile<T>(string filepath) where T : class { using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); return (T)serializer.Deserialize(stream); } } /// <summary> /// 反序列化XML字元串 /// </summary> public static T LoadFromXmlString<T>(string xml) where T : class { XmlSerializer serializer = new XmlSerializer(typeof(T)); byte[] bytes = Encoding.UTF8.GetBytes(xml); using (MemoryStream stream = new MemoryStream(bytes)) { return (T)serializer.Deserialize(stream); } } /// <summary> /// 序列化XML對象 /// </summary> public static string SaveToXmlString<T>(T entity) where T : class { using (MemoryStream stream = new MemoryStream()) { XmlSerializer serializer = new XmlSerializer(typeof(T)); serializer.Serialize(stream, entity); return Encoding.UTF8.GetString(stream.ToArray()); } } /// <summary> /// 序列化Json對象 /// </summary> public static string ToJsonString(object obj) { return ToJsonString<object>(obj); } /// <summary> /// 序列化Json對象 /// </summary> public static string ToJsonString<T>(T obj) where T : class { string text = JsonConvert.SerializeObject(obj); return text; } /// <summary> /// 反序列化Json字元串 /// </summary> public static T ToJsonObject<T>(string text) where T : class { T obj = (T)JsonConvert.DeserializeObject(text, typeof(T)); return obj; } }
如果向後臺傳入多個參數在data裡面用逗號分割可寫多個參數:
data: { corpName: corpName , corpName2: corpName2}