asp.net+js实现批量编码与解码的方法

357 查看

本文实例讲述了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程序设计有所帮助。