• 学习Professional ASP.NET 2.0(一)


    第一章以介绍asp.net2.0新特性为多
    第二章介绍了VS2005开发工具
    第三章介绍了如何创建应用程序,ASP.NET 网页代码模型 ,ASP.NET 网页指令 ,ASP.NET 页生命周期概述,跨页传递参数,ASP.NET 网站布局以及Global.asax

    第一章用两个例子比较了1.1和2.0数据访问的不同,第二个例子没有用到编程代码,只简单的使用控件就完成了数据访问的工作.
    列表1 Listing 01-01.aspx

    <%@ Page Language="VB" AutoEventWireup="True" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <script runat="server">

     Private Sub Page_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgs)
     If Not Page.IsPostBack Then
     BindData()
     End If
     End Sub

     Private Sub BindData()
     Dim conn As SqlConnection 
    = New SqlConnection("server='clingingboy';trusted_connection=true; Database='Northwind'")
     Dim cmd As SqlCommand 
    = New SqlCommand("Select * From Customers", conn)
     conn.Open()

     Dim da As SqlDataAdapter 
    = New SqlDataAdapter(cmd)
     Dim ds As New DataSet

     da.Fill(ds, 
    "Customers")

     DataGrid1.DataSource 
    = ds
     DataGrid1.DataBind()
     End Sub

     Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
     ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
     DataGrid1.CurrentPageIndex 
    = e.NewPageIndex
     BindData()
     End Sub

    </script>

    <html>
    <head>
    </head>
    <body>
     
    <form runat="server">
     
    <asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" OnPageIndexChanged="DataGrid1_PageIndexChanged">
     
    </asp:DataGrid>
     
    </form>
    </body>
    </html>
    列表2 Listing 01-02.aspx

    <%@ Page Language="VB" %>

    <script runat="server">

    </script>

    <html xmlns=http://www.w3.org/1999/xhtml>
    <head runat="server">
     
    <title>GridView Demo</title>
    </head>
    <body>
     
    <form runat="server">
     
    <asp:GridView ID="GridView1" Runat="server" AllowPaging="True" 
     DataSourceId
    ="Sqldatasource1" />
     
    <asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
     SelectCommand
    ="Select * From Customers"
     ProviderName
    ="System.Data.OleDb" 
     ConnectionString
    ="Provider=SQLOLEDB;Server=localhost;uid=sa;
     pwd=password;database=Northwind"
     />
     
    </form>
    </body>
    </html>

    第三章

    1.打开网站的方式

    uploads/200605/07_143940_snap1.gif


    3-1

    uploads/200605/07_143945_snap2.gif


    3-2

    uploads/200605/07_143948_snap3.gif


    3-3

    uploads/200605/07_143954_snap4.gif


    3-4

    2.ASP.NET 网页代码模型

    单文件页的优点
    通常,单文件模型适用于特定的页,在这些页中,代码主要由页中控件的事件处理程序组成。

    单文件页模型的优点包括以下几点:

    在没有太多代码的页中,可以方便地将代码和标记保留在同一个文件中,这一点比代码隐藏模型的其他优点都重要。例如,由于可以在一个地方看到代码和标记,因此研究单文件页更容易。

    因为只有一个文件,所以使用单文件模型编写的页更容易部署或发送给其他程序员。

    由于文件之间没有相关性,因此更容易对单文件页进行重命名。

    因为页自包含于单个文件中,故而在源代码管理系统中管理文件稍微简单一些。

    代码隐藏页的优点
    代码隐藏页的优点使它们适用于包含大量代码或多个开发人员共同创建网站的 Web 应用程序。

    代码隐藏模型的优点包括以下几点:

    代码隐藏页可以清楚地分隔标记(用户界面)和代码。这一点很实用,可以在程序员编写代码的同时让设计人员处理标记。

    代码并不会向仅使用页标记的页设计人员或其他人员公开。

    代码可在多个页中重用。
    (1).单文件页模型
    <%@ Page Language="C#" %>
    <script runat="server">
    void Button1_Click(Object sender, EventArgs e){ Label1.Text = "Clicked at " + DateTime.Now.ToString();}
    </script>
    <html>
    <head>
     
    <title>Single-File Page Model</title>
    </head>
    <body>
     
    <form runat="server">
     
    <div>
     
    <asp:Label id="Label1" 
     runat
    ="server" Text="Label">
     
    </asp:Label>
     
    <br />
     
    <asp:Button id="Button1" 
     runat
    ="server" 
     onclick
    ="Button1_Click" 
     Text
    ="Button">
     
    </asp:Button>
     
    </div>
     
    </form>
    </body>
    </html>
    (2)代码隐藏页模型

    <%@ Page Language="C#" CodeFile="SamplePage.aspx.cs" 
     Inherits
    ="SamplePage" AutoEventWireup="true" 
    %>
    <html>
    <head runat="server" >
     
    <title>Code-Behind Page Model</title>
    </head>
    <body>
     
    <form id="form1" runat="server">
     
    <div>
     
    <asp:Label id="Label1" 
     runat
    ="server" Text="Label" >
     
    </asp:Label>
     
    <br />
     
    <asp:Button id="Button1" 
     runat
    ="server" 
     onclick
    ="Button1_Click" 
     Text
    ="Button" >
     
    </asp:Button>
     
    </div>
     
    </form>
    </body>
    </html>

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    public partial class SamplePage : System.Web.UI.Page
    {
     protected void Button1_Click(object sender, EventArgs e)
     {
     Label1.Text = "Clicked at " + DateTime.Now.ToString();
     }
    }


    3.ASP.NET 网页指令


    指令指定一些设置,由页和用户控件编译器在处理 ASP.NET Web 窗体页(.aspx 文件)和用户控件 (.ascx) 文件时使用这些设置。

    ASP.NET 将任何不包含显式指令名称的指令块 (<%@ %>) 当作 @ Page 指令(对于页)或 @ Control 指令(对于用户控件)来进行处理。

    有关可用于每个指令的属性的语法信息和描述,请使用下表中列出的链接。

    uploads/200605/07_144805_snap5.gif


    3-5

    指令 说明
    @ Assembly
    以声明方式将程序集链接到当前页或用户控件。

    @ Control
    定义 ASP.NET 页分析器和编译器使用的控件特定的属性;只能包含在 .ascx 文件(用户控件)中。

    @ Implements
    以声明方式指示页或用户控件实现指定的 .NET Framework 接口。

    @ Import
    将命名空间显式导入页或用户控件中。

    @ Master
    将页标识为母版页,并定义 ASP.NET 页分析器和编译器使用的属性;只能包含在 .master 文件中。

    @ MasterType
    定义用于确定页的 Master 属性类型的类或虚拟目录。

    @ OutputCache
    以声明方式控制页或用户控件的输出缓存策略。

    @ Page
    定义 ASP.NET 页分析器和编译器使用的页特定的属性;只能包含在 .aspx 文件中。

    @ PreviousPageType
    创建一个强类型的引用,该引用指向来自跨页发送的目标的源页。

    @ Reference
    以声明方式将页、用户控件或 COM 控件链接到当前的页或用户控件。

    @ Register
    将别名与命名空间和类相关联,以便在用户控件和自定义服务器控件被纳入到请求页或用户控件中时得以呈现。

    具体属性要参考MSDN

    4.ASP.NET 页生命周期概述

    页事件 典型使用
    Page_PreInit
    使用 IsPostBack 属性确定是否是第一次处理该页。

    创建或重新创建动态控件。

    动态设置主控页。

    动态设置 Theme 属性。

    读取或设置配置文件属性值。

    Page_Init
    读取或初始化控件属性。

    Page_Load
    读取和更新控件属性。

    Control events
    执行特定于应用程序的处理:

    如果页包含验证程序控件,请在执行任何处理之前检查页和各个验证控件的 IsValid 属性。

    处理特定事件,如 Button 控件的 Click 事件。

    Page_PreRender
    对页的内容进行最后更改。

    Page_Unload
    执行最后的清理工作,可能包括:

    关闭打开的文件和数据库连接。

    完成日志记录或其他特定于请求的任务。

    动态改变theme

    <script runat=”server”>
    protected 
    void Page_PreInit(object sender, System.EventArgs e)
    {
    Page.Theme 
    = Request.QueryString[“ThemeChange”];
    }

    </script>


    5.跨页传递参数

    注意指定了PostBackUrl值 获取或设置单击 Button 控件时从当前页发送到的网页的 URL
    Page1.aspx

    <%@ Page Language="C#" %>

    <!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 id="Head1" runat="server">
     
    <title>First Page</title>
    </head>
    <body>
     
    <form id="form1" runat="server">
     Enter your name:
    <br />
     
    <asp:Textbox ID="TextBox1" Runat="server">
     
    </asp:Textbox>
     
    <p>
     When do you want to fly?
    <br />
     
    <asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar></p>
     
    <br />
     
    <asp:Button ID="Button2" Runat="server" Text="Submit page to Page2.aspx"
     PostBackUrl
    ="Page2.aspx" />
     
    <p>
     
    <asp:Label ID="Label1" Runat="server"></asp:Label></p>
     
    </form>
    </body>
    </html>

    Page.PreviousPage 属性 获取向当前页传输控件的页,使用FindControl 方法在页命名容器中搜索指定的服务器控件。
    Page2.aspx

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server"> 
     protected 
    void Page_Load(object sender, System.EventArgs e)
     
    {
     TextBox pp_Textbox1;
     Calendar pp_Calendar1;
     
     pp_Textbox1 
    = (TextBox)PreviousPage.FindControl("Textbox1");
     pp_Calendar1 
    = (Calendar)PreviousPage.FindControl("Calendar1");
     
     Label1.Text 
    = "Hello " + pp_Textbox1.Text + "<br />" + "Date Selected: " + 
     pp_Calendar1.SelectedDate.ToShortDateString();
     }
     
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
     
    <title>Second Page</title>
    </head>
    <body>
     
    <form id="form1" runat="server">
     
    <asp:Label ID="Label1" Runat="server"></asp:Label>
     
    </form>
    </body>
    </html>

    上面方法可以使用另一种方法获取值,可以先定义属性来获取控件ID

    更改page1.aspx

    加上以下代码,其他不变
    public TextBox pp_TextBox1
     {
     get
     {
     return TextBox1;
     }
     }

     public Calendar pp_Calendar1
     {
     get
     {
     return Calendar1;
     }
     }

    注意
    <%@ PreviousPageType VirtualPath="~/Cross Page Posting/Page1.aspx" %>
    创建一个强类型的引用,该引用指向来自跨页发送的目标的源页
    page2.aspx

    <%@ Page Language="C#" %>
    <%@ PreviousPageType VirtualPath="~/Cross Page Posting/Page1.aspx" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server"> 
     protected 
    void Page_Load(object sender, System.EventArgs e)
     
    {
     Label1.Text 
    = "Hello " + PreviousPage.pp_TextBox1.Text + "<br />" +
     
    "Date Selected: " +
     PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
     }
     
    </script>

    还可以指定IsCrossPagePostBack值获取一个值,该值指示跨页回发中是否涉及该页

    6.ASP.NET 网站布局

    ASP.NET 识别您可用于特定类型的内容的某些文件夹名称。下表列出了保留的文件夹名称以及文件夹中通常包含的文件类型。

    文件夹 说明
    App_Browsers
    包含 ASP.NET 用于标识个别浏览器并确定其功能的浏览器定义 (.browser) 文件。有关更多信息,请参见浏览器定义文件架构(browsers 元素)和如何:在 ASP.NET 网页中检测浏览器类型。

    App_Code
    包含您希望作为应用程序一部分进行编译的实用工具类和业务对象(例如 .cs、.vb 和 .jsl 文件)的源代码。在动态编译的应用程序中,当对应用程序发出首次请求时,ASP.NET 编译 App_Code 文件夹中的代码。然后在检测到任何更改时重新编译该文件夹中的项。

    注意
    可以在 App_Code 文件夹中放置任意文件类型以创建强类型对象。例如,将 Web 服务文件(.wsdl 和 .xsd 文件)放置在 App_Code 文件夹可以创建强类型的代理。


    在应用程序中将自动引用 App_Code 文件夹中的代码。此外,App_Code 文件夹可以包含需要在运行时编译的文件的子目录。有关更多信息,请参见 ASP.NET 网站中的共享代码文件夹和 compilation 的 codeSubDirectories 元素(ASP.NET 设置架构)。

    App_Data
    包含应用程序数据文件,包括 MDF 文件、XML 文件和其他数据存储文件。ASP.NET 2.0 使用 App_Data 文件夹来存储应用程序的本地数据库,该数据库可用于维护成员资格和角色信息。有关更多信息,请参见成员资格简介和了解角色管理。

    App_GlobalResources
    包含编译到具有全局范围的程序集中的资源(.resx 和 .resources 文件)。App_GlobalResources 文件夹中的资源是强类型的,可以通过编程方式进行访问。有关更多信息,请参见 ASP.NET 网页资源概述。

    App_LocalResources
    包含与应用程序中的特定页、用户控件或母版页关联的资源(.resx 和 .resources 文件)。有关更多信息,请参见 ASP.NET 网页资源概述。

    App_Themes
    包含用于定义 ASP.NET 网页和控件外观的文件集合(.skin 和 .css 文件以及图像文件和一般资源)。有关更多信息,请参见 ASP.NET 主题和外观概述。

    App_WebReferences
    包含用于定义在应用程序中使用的 Web 引用的引用协定文件(.wsdl 文件)、架构(.xsd 文件)和发现文档文件(.disco 和 .discomap 文件)。有关生成 XML Web Services 的代码的更多信息,请参见 Web 服务描述语言工具 (Wsdl.exe)。

    Bin
    包含您要在应用程序中引用的控件、组件或其他代码的已编译程序集(.dll 文件)。在应用程序中将自动引用 Bin 文件夹中的代码所表示的任何类。有关更多信息,请参见 ASP.NET 网站中的共享代码文件夹。

    以上为主要内容,细节的东西无法一一列出
  • 相关阅读:
    C#仿制QQ弹出消息框
    Winform下载文件
    asp.net文件下载
    C#FileStream复制大文件【转自www.bitsCN.com】
    C#解压或压缩文件夹
    TreeView无级级绑定
    C# WinForm窗口最小化到系统托盘
    [C#]实现序列号生成器
    VS2005小技巧收集(一)
    Failed TO CREATE LOGFILE GROUP解决
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/393348.html
Copyright © 2020-2023  润新知