本文主要實作無動態重繪查詢后臺資料功能,主要用到ajax+ashx+sqlserver進行互動.
首先需要參考Jquery:
<script language="javascript" type="text/javascript" src="https://www.cnblogs.com/hbqjzx/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}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/12325.html
標籤:ASP.NET
