最近剛剛接手B/S的項目,涉及到了服務器從數據庫中查詢數據,然后根據數據拼出前台的HTML語句。下面是自己的一段路程。
從最開始自己的一個技術難點,后台拼接完了HTML語句,怎么送到前台呢?下面是關於ASPX頁面的數據交互。注意:如果代碼看不清,請選中看觀看。
最簡單的一種方法: <%=方法名()%>,這個方法名是在后台寫的方法。看圖
前台aspx頁面。
后台的代碼
第二種:使用jQuery封裝好的ajax
前台js代碼:注意一定要引用jQuery庫。
<span style="font-size:18px;"> $.ajax({ url: "MobileFeedBack.aspx/SubmitSuggestion", //url 前面是aspx的頁文件,后面是調用的方法名 type: "post", data: params, contentType: "application/json;charset=utf-8", success: function (data) { if (data == '{"d":true}') { mui.alert("提交失敗,請稍等!", "提示"); } else { mui.alert('提交成功,感謝您的支持!', '提示', function () { var url = "../mobile/mobileHomePage.aspx"; window.location.href = url; }); } } });</span>
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using BLL; using Entity; using System.Web.Services; using System.Web.Script.Serialization;</span>
<span style="font-size:18px;">[WebMethod] //使客戶端可以調用此方法,基於引用System.Web.Services public static bool SubmitSuggestion(string suggestion, string userName) { //語句體 }</span>
js的post——>一般處理程序——>后台類的方法——>一般處理程序——>js的post
在此要注意:FeedBack.ashx是一般處理程序的文件名,要注意路徑。
$.post("FeedBack.ashx", { Message: sendMessage, Grade: grade }, function (data) { mui.alert('提交成功,感謝您的支持','提示',function(){ window.top.location.href = '../LoginMobile.aspx'; }); }, 'json');
后台代碼
//using引用 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI.WebControls; using System.Text;
//用來獲取客戶端http請求的信息 ,該方法由系統自動生成,語句體自己寫 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html;charset=utf-8"; string sendmessage = context.Request.Params["Message"].ToString(); string grade = context.Request.Params["Grade"].ToString(); string json = reString(sendmessage, grade); //在此調用了其他的方法。 context.Response.Write(json);//返回處理結果 context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.End(); }
public string reString(string message,string grade) { weChatBackInfo feedback = new weChatBackInfo(); //先聲明一個類 StringBuilder sb = new StringBuilder(); if (feedback.Submit(message, grade)) //括號里調用了該類的方法 { sb.Append("我們已接收到您的反饋意見,感謝您的支持"); } else { sb.Append("感謝您的支持,網絡繁忙,請稍后"); } //json串的格式: {鍵名:值,鍵名:值。。。。。。} string jsonString = "{\"returnMessage\":\"" + sb.ToString() + "\"}"; return jsonString; }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。