Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/9 1:36:23
asp.net2.0中Server对象的方法和属性

asp.net2.0中Server对象的方法和属性

Server对象的属性

Server对象常用属性及说明如表1所示。



表1 Server对象常用属性及说明

下面对比较重要的属性进行详细介绍。

MachineName属性。用来获取服务器的计算机名称。

* 示例

使用MachineName属性获取服务器名称

本示例实现的是,当程序运行时,在页面Default.aspx中显示服务器名称。示例运行结果如图1所示。



图1 使用MachineName属性获取服务器名称

程序代码如下。

在Default.aspx页面的Page_Load事件中,调用Server对象的MachineName属性获取服务器名称,然后使用ToLower方法将得到的服务器名称转换为小写,在Label控件中显示出来,实现代码如下:

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = Server.MachineName.ToLower();

}

完整程序代码如下:

★ ★★★★Default.aspx代码文件完整程序代码★★★★★

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>







无标题页









服务器名称:








★ ★★★★Default.aspx.cs代码文件完整程序代码★★★★★

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = Server.MachineName.ToLower();

}

}

Server对象的方法

Server对象常用方法及说明如表1所示。



表2Server对象常用方法及说明

下面对比较重要的方法进行详细介绍。

(1)HtmlDecode和HtmlEncode方法。HtmlDecode方法用于对HTML编码的字符串进行解码,并返回已解码的字符串。

语法:

public string HtmlDecode (string s)

参数说明如下。

s:要解码的HTML字符串。

返回值:已解码的文本。

HtmlEncode方法用于对字符串进行HTML编码,并返回已编码的字符串。

语法:

public string HtmlEncode (string s)

参数说明如下。

s:要编码的文本字符串。

返回值:已使用HTML编码的文本。

* 示例

使用HtmlDecode和HtmlEncode方法编码相同字符串

本示例实现的是当程序运行时,在页面Default.aspx中分别输出使用Server对象的HtmlEncode方法和HtmlDecode方法解码和编码的相同字符串。示例运行结果如图1所示。



图2 使用HtmlDecode和HtmlEncode方法编码相同字符串

程序代码如下。

在Default.aspx页面的Page_Load事件中,分别调用Server对象的HtmlEncode方法和HtmlDecode方法对相同的字符串进行编码和解码,然后在页面中输出,实现代码如下:

protected void Page_Load(object sender, EventArgs e)

{

Response.Write("使用HtmlEncode方法为指定字符串编码:" + Server.HtmlEncode("浏览器中字符串") + "
"

+ "使用HtmlDecode方法为指定字符串解码:" + Server.HtmlDecode("浏览器中字符串"));


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759