ASP.NET基礎

ASP.NET 從入門到精通

 

 

ASP.NET的內置對象

Response

 

Default.aspx.cscss

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx?Name=Michael&Sex=先生");
    }
}

 

Welcome.aspx.cshtml

public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Params["Name"];
        string sex = Request.Params["Sex"];
        Response.Write("歡迎" + name + sex + "!");
    }
}

 

Request

經常使用屬性瀏覽器

ApplicationPath服務器

Cookiescookie

Params框架

 

MapPath 將請求的URL路徑映射到服務器的物理路徑ui

 

頁面間傳值

Request["value"]編碼

Request.Params["value"]spa

Request.QueryString["value"]code

獲取瀏覽器信息

HttpBrowserCapabilities b = Request.Browser;
        Response.Write("客戶端瀏覽器信息:");
        Response.Write("<hr>");
        Response.Write("類型:" + b.Type + "<br>");
        Response.Write("名稱:" + b.Browser + "<br>");
        Response.Write("版本:" + b.Version + "<br>");
        Response.Write("操做平臺:" + b.Platform + "<br>");
        Response.Write("是否支持框架" + b.Frames + "<br>");
        Response.Write("是否支持表格" + b.Tables + "<br>");
        Response.Write("是否支持cookie" + b.Cookies + "<br>");
        Response.Write("<hr");

 

 

Application

集合

Contents

StaticObjects

屬性

AllKeys

方法

Application.Lock();
        Application["Name"] = "XiaoLiang";
        Application.UnLock();
        Response.Write("Application[\"Name\"的值爲" + Application["Name"].ToString());

 

 

Session對象

會話變量,也能夠經過鍵值對來處理變量

 

Cookie對象

屬性

Expires

Name

Value

Path

 

方法

Equals

ToString

TimeSpan ts = new TimeSpan(0, 0, 20, 0);
Response.Cookies["myCookie"].Expires = DateTime.Now.Add(ts);//20分鐘後過時
Response.Cookies["myCookie"].Expires = DateTime.Now.AddMonths(1);//一個月後過時
Response.Cookies["myCookie"].Expires = DateTime.Parse("10/26/2017");//指定日期
Response.Cookies["myCookie"].Expires = DateTime.MaxValue;//永不過時
Response.Cookies["myCookie"].Expires = DateTime.MinValue;//關閉瀏覽器後過時

 

 

Server對象

與Web服務器相關的類

MachineName 獲取服務器的計算機名

ScriptTimeout 獲取和設置請求超時值

 

Execute 在當前請求的上下文中執行指定資源的處理程序

HtmlDecode 對已被編碼以消除無效html字符的字符串進行解碼

HtmlEnCode

UrlDecode

UrlEncode

 

MapPath

transfer 終止當前頁的執行,併爲當前請求開始執行新頁

Response.Write("PATH:" + Server.MapPath("Default.aspx"));

 

控件

Label

<asp:Label ID="Label1" runat="server" BackColor="Red" BorderColor="#400040" BorderWidth="2px" Font-Bold="true" ForeColor="Chartreuse" Height="20px" Text="Label外觀設置示例" Width="187px"></asp:Label>

 

 

經過css樣式設置label控件的外觀

 

 

數據驗證技術

非空驗證
RequiredFieldValidator

數據比較驗證 
CompareValidator
兩次輸入的密碼是否相同
數據類型驗證


數據格式驗證
RegularExpressionValidator

數據範圍驗證控件
RangeValidator

錯誤信息顯示控件
ValidationSummary

自定義驗證控件
CustomValidator

母板頁

 

 

建立母板頁

 

添加新項,母板頁

MasterPage.master

建立內容頁

 

嵌套母板頁

 

第9章 數據綁定

數據綁定表達式都必須包含在 <%# %>中,執行綁定操做要麼執行page對象的DataBind方法,要麼執行數據綁定控件對應類的實力對象的DataBind方法。

 

屬性綁定

<div>
        簡單屬性綁定<br/>
        商品名稱: <%# GoodsName %>
        商品種類: <%# GoodsKind %>    
    </div>

 

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
    }
    public string GoodsKind
    {
        get {
            return "家用電器";
        }
    }
    public string GoodsName
    {
        get {
            return "彩色電視機";
        }
    }
}

表達式綁定

核心代碼

<asp:Label ID="Label1" runat="server" Text='<%#"總金額爲:"+ Convert.ToString(Convert.ToDecimal (TextBox1.Text)*Convert.ToInt32(TextBox2.Text)) %>'></asp:Label></td>
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
    }
}

 

<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2">
    表達式綁定</td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 82px">
                    單價:</td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox></td>
                <td style="width: 100px">
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="輸入數字" ControlToValidate="TextBox1" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td style="width: 82px">
                    數量:</td>
                <td style="width: 100px">
                    <asp:TextBox ID="TextBox2" runat="server">0</asp:TextBox></td>
                <td style="width: 100px">
                    <asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="輸入數字" ControlToValidate="TextBox2" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnOk" runat="server" Text="肯定" /></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="Label1" runat="server" Text='<%#"總金額爲:"+ Convert.ToString(Convert.ToDecimal (TextBox1.Text)*Convert.ToInt32(TextBox2.Text)) %>'></asp:Label></td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

集合綁定

<div>
<asp:DropDownList runat="server" ID="FruitDropDownList"></asp:DropDownList>
</div>

 

protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
        arrayList.Add("香蕉");
        arrayList.Add("蘋果");
        FruitDropDownList.DataSource = arrayList;
        FruitDropDownList.DataBind();
    }

 

方法調用結果綁定

 

核心代碼

<asp:Label ID="Label1" runat="server" Text='<%#operation(ddlOperator.SelectedValue) %>'/></td>

 

<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<table>
            <tr>
                <td colspan="2">
                    綁定方法調用的結果</td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    第一個數:</td>
                <td style="width: 100px">
                    <asp:TextBox ID="txtNum1" runat="server" Width="60px">0</asp:TextBox></td>
                <td style="width: 100px">
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNum1"
                        ErrorMessage="輸入數字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    第二個數:</td>
                <td style="width: 100px">
                    <asp:TextBox ID="txtNum2" runat="server" Width="60px">0</asp:TextBox></td>
                <td style="width: 100px">
                    <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtNum2"
                        ErrorMessage="輸入數字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td>
            </tr>
            <tr>
                <td style="width: 100px; height: 24px">
                    運算符號:</td>
                <td style="width: 100px; height: 24px">
                    <asp:DropDownList ID="ddlOperator" runat="server">
                        <asp:ListItem>--請選擇運算符號--</asp:ListItem>
                        <asp:ListItem>+</asp:ListItem>
                        <asp:ListItem>-</asp:ListItem>
                        <asp:ListItem>*</asp:ListItem>
                        <asp:ListItem>/</asp:ListItem>
                    </asp:DropDownList></td>
                <td style="width: 100px; height: 24px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                    <asp:Button ID="btnOk" runat="server" Text="肯定" /></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    運算結果:</td>
                <td style="width: 100px">
                    <asp:Label ID="Label1" runat="server" Text='<%#operation(ddlOperator.SelectedValue) %>'/></td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

 

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)
    {       
            Page.DataBind();
    }
    public string operation(string VarOperator)
    {
        double num1=Convert.ToDouble (txtNum1.Text);
        double num2=Convert.ToDouble (txtNum2.Text);
        double result = 0;
        switch (VarOperator)
        {
            case "+":
                result = num1 + num2;
                break ;
            case "-":
                result = num1 - num2;
                break ;
            case "*":
                result = num1 * num2;
                break ;
            case "/":
                result = num1 / num2;
                break ;
        }
        return result.ToString ();
    }
}
相關文章
相關標籤/搜索