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 ();
    }
}
相关文章
相关标签/搜索