網域查詢: www.
返回首頁

ASP.NET中的HTML編碼和URL編碼

時間:2010-02-17 02:36來源: 作者: 點擊:
導 讀︰Server.HTMLEncode 和 Server.URLEncode 是ASP中很常用的函數,在ASP.NET中也有類似的函數︰HtmlEncode 和 UrlEncode (注意大小寫)以下用實例來進行介紹。 Server.HTMLEncode and Server.URLEncode are ve
  導 讀︰Server.HTMLEncode 和 Server.URLEncode 是ASP中很常用的函數,在ASP.NET中也有類似的函數︰HtmlEncode 和 UrlEncode (注意大小寫)以下用實例來進行介紹。

Server.HTMLEncode and Server.URLEncode are very common functions used
by ASP developers. Well, as you may have guessed, these two functions
have ASP.NET counterparts. HtmlEncode and UrlEncode (notice case) are
part of the System.Web.HttpUtility namespace.

Here is the demo

urlencode.asp?test=1&test1=2
urlencode.asp%3ftest%3d1%26test1%3d2


This is a test & hopefully it works!
<h1>This is a test & hopefully it works!</h1>

**********************************
Here is the code:
**********************************

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.HttpUtility" %>
<html>
<head>
<title>HTMLEncode and URLEncode the ASP.NET Way</title>

<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)

''Assign our test url to a variable, assign the variable to our
label control
Dim strURLText as String = "urlencode.asp?test=1&test1=2"
strURL.Text = strURLText

''Encode the test url, assign it to a variable, assign the
variable to our label control
Dim strURLEncodeText As String = UrlEncode(strURLText)
strURLEncode.Text = strURLEncodeText

''Assign our test url to a variable, assign the variable to our
label control
Dim strHTMLText As String = "<h1>This is a test & hopefully it
works!</h1>"
strHTML.Text = strHTMLText

''Encode the test html, assign it to a variable, assign the
variable to our label control
Dim strHTMLEncodeText As String = HtmlEncode(strHTMLText)
strHTMLEncode.Text = strHTMLEncodeText

End Sub
</script>
</head>
<body>
<asp:label id="strURL" forecolor="red" font-bold="true" runat=server/>
<p>
<asp:label id="strURLEncode" forecolor="blue" font-bold="true" runat=server/>
<p>
<asp:label id="strHTML" forecolor="red" font-bold="true" runat=server/>
<p>
<asp:label id="strHTMLEncode" forecolor="blue" font-bold="true" runat=server/>


</body>
</html>


頂一下
(0)
0%
踩一下
(0)
0%
------分隔線----------------------------
最新評論 查看所有評論
發表評論 查看所有評論
請自覺遵守互聯網相關的政策法規,嚴禁發佈色情、暴力、反動的言論。
評價:
表情:
用戶名: 密碼: 驗證碼:
推薦內容