堅持學asp.net——(十)
來源:互聯網
上載者:User
asp.net 一、命名空間
只是一個形象的代號,能夠粗略表達其中類的情況。
下面列舉在使用asp.net建立web程式時最常用的命名空間:
System.Web;System.Web.Ui;System.IO;System.Collections;System.Diagnostics;System.Date;System.Globalization;System.Drawing;System.Xml
Response.Redirect的使用:
<script language="c#" runat="server">
void Page_Load(object source,EventArgs e){
if(!(IsPostBack)){
Button1.Text="OK";
DropDownList1.Items.Add("http://www.sohu.com");
DropDownList1.Items.Add("http://www.sina.com.cn");
DropDownList1.Items.Add("http://www.163.com");
}
}
public void Click(object o,EventArgs e){
Response.Redirect(DropDownList1.SelectedItem.Text);
}
</script>
<form runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Click" Text=""/>
</form>
二、asp.net的核心對象
1、Request對象:
部分屬性:
ApplicationPath
Path
PhysicalApplicationPath
Browser
Cookies
IsSecureConnection
RequestType
QueryString
Url
Rawurl
UserHostName
UserHostAddress
UserLanguages
下面是一個檢查這些屬性的小例子:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<script Language="C#" runat="server">
void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack) {
if(Request.Browser.Browser=="IE")
{
if(Request.Browser.MajorVersion<6){
Label1.Text="Time to update your Ietnet Explore !";
}
else{
Label1.Text="小子很吊呀,早就用上最新的瀏覽器了!";
}
}
else
{
Label1.Text="Your Browser is "+Request.Browser.Browser+" !";
}
}
}
</script>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<asp:Label ID="Label1" runat="server" Text="" />
</body>
</html>
2、Response對象
以下幾個屬性和方法:
Buffer
ContentType
Cookies
Clear()
Flush()
End()
Redirect()
Write()
WriteFile()
下面是一個形象的例子:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無標題文檔</title>
</head>
<body>
<%
for(int i=0;i<50000;i++){
Response.Write("X");
if(i%300==0)
Response.Write("<br>");
}
%>
</body>
</html>
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>無標題文檔</title>
</head>
<body>
<%
Response.Buffer=false;
for(int i=0;i<50000;i++){
Response.Write("X");
if(i%300==0)
Response.Write("<br>");
}
%>
</body>
</html>
這兩段代碼十分想象,但是運行差別卻是非常明顯的。
3、server對象
MachineName
ScriptTimeOut
HtmlEncode()
HtmlDecode()
MapPath()
UrlEncode()
UrlDecode()