本文實例講述了asp.net+js實現批量編碼與解碼的方法。分享給大家供大家參考,具體如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> <mce:script src="jquery-1.3.2.min.js" mce_src="jquery-1.3.2.min.js" type="text/javascript"></mce:script> <mce:script type="text/javascript"><!-- var data = {}; data.Id = 1; data.Name = "1 & 2"; data.Desc = "<b> def </b>"; var clientMsg = ""; clientMsg+="<font color='green'>1: 客戶端編碼之前:<br/>"; for (var name in data) { clientMsg+=(name + ":" + data[name] + "<br/>"); } clientMsg += "1: 客戶端編碼之後:<br/>"; escapeData(data); for (var name in data) { clientMsg+=name + ":" + data[name] + "<br/>"; } clientMsg += "</font>"; $.post("Default.aspx", data, function (data) { document.write(clientMsg+"<br/>"+data); }); function escapeData(data) { for (var name in data) { data[name] = escape(data[name]); } } // --></mce:script> </head> <body> <form id="form1" runat="server"> <div id="divClient" style="color:Green;" mce_style="color:Green;" > </div> </form> </body> </html>
C#代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request["Id"]!=null) { string msg = ""; msg += "<font color='red'>服務器得到的值(解碼前):<br/>"; foreach (string s in Request.Form.AllKeys) { msg += string.Format("{0}:{1}<br/>", s, Request.Form[s]); } msg += "服務器得到的值(解碼後):<br/>"; foreach (string s in Request.Form.AllKeys) { msg += string.Format("{0}:{1}<br/>", s, HttpUtility.UrlDecode(Request.Form[s])); } msg += "</font>"; msg += string.Format("未編碼的字符解碼1ab&<b>小</b>:{0}",HttpUtility.UrlDecode("1ab&<b>小</b>")); ; Response.Write(msg); Response.End(); } } }
希望本文所述對大家asp.net程序設計有所幫助。