asp.net简述Web Forms开发模式

详情请查阅:http://www.runoob.com/aspnet/aspnet-intro.html

1.Web Forms 是三种创建 ASP.NET 网站和 Web 应用程序的编程模式中的一种。

 Web Forms 是最古老的 ASP.NET 编程模式,是整合了 HTML、服务器控件和服务器代码的事件驱动网页。

 Web Forms 是在服务器上编译和执行的,再由服务器生成 HTML 显示为网页。

 Web Forms 有数以百计的 Web 控件和 Web 组件用来创建带有数据访问的用户驱动网站。

2.Web Forms - HTML 页面

 转换 HTML 页面为 ASP.NET 页面最简单的方法是,直接复制一个 HTML 文件,并把新文件的扩展名改成 .aspx 。

 

<html>
<body bgcolor="yellow">
<center>
<h2>Hello RUNOOB.COM!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>

ASP.NET 对比经典 ASP

3.Web Forms - 服务器控件

 服务器控件是服务器可理解的标签。

有三种类型的服务器控件:

  • HTML 服务器控件 - 创建的 HTML 标签
  • Web 服务器控件 - 新的 ASP.NET 标签
  • Validation 服务器控件 - 用于输入验证

ASP.NET - HTML 服务器控件

HTML 服务器控件是服务器可理解的 HTML 标签。

ASP.NET 文件中的 HTML 元素,默认是作为文本进行处理的。要想让这些元素可编程,需向 HTML 元素中添加 runat="server" 属性。这个属性表示,该元素将被作为服务器控件进行处理。同时需要添加 id 属性来标识服务器控件。id 引用可用于操作运行时的服务器控件。

注释:所有 HTML 服务器控件必须位于带有 runat="server" 属性的 <form> 标签内。runat="server" 属性表明了该表单必须在服务器上进行处理。同时也表明了包含在它内部的控件可被服务器脚本访问。

4.ASP.NET Web Forms - 事件

事件句柄是一种针对给定事件来执行代码的子例程。

5.ASP.NET Web Forms - HTML 表单

所有的服务器控件都必须出现在 <form> 标签中,<form> 标签必须包含 runat="server" 属性

ASP.NET Web 表单

所有的服务器控件都必须出现在 <form> 标签中,<form> 标签必须包含 runat="server" 属性。runat="server" 属性表明该表单必须在服务器上进行处理。同时也表明了包含在它内部的控件可被服务器脚本访问:

<form runat="server">

...HTML + server controls

</form>

注释:该表单总是被提交到自身页面。如果指定了一个 action 属性,它会被忽略。如果省略了 method 属性,它将会默认设置 method="post"。同时,如果没有指定 name 和 id 属性,它们会由 ASP.NET 自动分配。

注释:一个 .aspx 页面只能包含一个 <form runat="server"> 控件!

提交表单

表单通常通过点击按钮来提交。ASP.NET 中的 Button 服务器控件的格式如下:

<asp:Button id="id" text="label" OnClick="sub" runat="server" />
id 属性为按钮定义了一个唯一的名称,text 属性为按钮分配了一个标签。onClick 事件句柄规定了一个要执行的已命名的子例程。
<script  runat="server">
Sub submit(Source As Object, e As EventArgs)
   button1.Text="You clicked me!"
End Sub
</script>

<html>
<body>

<form runat="server">
<asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit" />
</form>

</body>
</html>

6.ASP.NET Web Forms - 维持 ViewState

通过在 Web Form 中维持对象的 ViewState(视图状态),可以省去大量的编码工作。

维持 ViewState(视图状态)

在 ASP .NET 中,当一个表单被提交时,表单会连同表单值一起出现在浏览器窗口中。如何做到的呢?这是因为 ASP .NET 维持了 ViewState。 ViewState 会在页面被提交到服务器时表明它的状态。这个状态是通过在带有 <form runat="server"> 控件的每个页面上放置一个隐藏域定义的。

维持 ViewState 是 ASP.NET Web Forms 的默认设置。如果想不维持 ViewState,请在 .aspx 页面顶部包含指令 <%@ Page EnableViewState="false" %> ,或者向任意控件添加属性 EnableViewState="false" 。

7.ASP.NET Web Forms - TextBox 控件

TextBox 控件用于创建用户可输入文本的文本框。

TextBox 控件

8.ASP.NET Web Forms - Button 控件

Button 控件用于显示一个下压按钮。下压按钮可能是一个提交按钮或者是一个命令按钮。在默认情况下,这个控件是提交按钮。

提交按钮没有命令名称,当它被点击时,它会把页面传回到服务器。可以编写一些事件句柄,当提交按钮被点击时,用来控制动作的执行。

命令按钮有命令名称,并且允许在页面上创建多个 Button 控件。可以编写一些时间句柄,当命令按钮被点击时,用来控制动作的执行。

添加脚本

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

9.ASP.NET Web Forms - 数据绑定

可以使用数据绑定(Data Binding)来完成带可选项的列表,这些可选项来自某个导入的数据源,比如数据库、XML 文件或者脚本。

数据绑定

下面的控件是支持数据绑定的列表控件:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

以上每个控件的可选项通常是在一个或者多个 asp:ListItem 控件中定义,如下:

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="countrylist" runat="server">
<asp:ListItem value="N" text="Norway" />
<asp:ListItem value="S" text="Sweden" />
<asp:ListItem value="F" text="France" />
<asp:ListItem value="I" text="Italy" />
</asp:RadioButtonList>
</form>

</body>
</html>

10.ASP.NET Web Forms - ArrayList 对象

ArrayList 对象是包含单个数据值的项目的集合。

创建 ArrayList

ArrayList 对象是包含单个数据值的项目的集合。

通过 Add() 方法向 ArrayList 添加项目。

下面的代码创建了一个名为 mycountries 的 ArrayList 对象,并添加了四个项目:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
end if
end sub
</script>

在默认情况下,一个 ArrayList 对象包含 16 个条目。可通过 TrimToSize() 方法把 ArrayList 调整为最终尺寸

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
end if
end sub
</script>

通过 Sort() 方法,ArrayList 也能够按照字母顺序或者数字顺序进行排序:

mycountries.Sort()

要实现反向排序,请在 Sort() 方法后应用 Reverse() 方法:

mycountries.Reverse()

绑定数据到 ArrayList

ArrayList 对象可为下列的控件自动生成文本和值:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素):

然后添加创建列表的脚本,并且绑定列表中的值到 RadioButtonList 控件:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New ArrayList
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
rb.DataSource=mycountries
rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>

</body>
</html>

RadioButtonList 控件的 DataSource 属性被设置为该 ArrayList,它定义了这个 RadioButtonList 控件的数据源。RadioButtonList 控件的 DataBind() 方法把 RadioButtonList 控件与数据源绑定在一起。

注释:数据值作为控件的 Text 和 Value 属性来使用。如需添加不同于 Text 的 Value,请使用 Hashtable 对象或者 SortedList 对象。

11.ASP.NET Web Forms - Hashtable 对象

Hashtable 对象包含用键/值对表示的项目。

http://www.runoob.com/aspnet/aspnet-hashtable.html

创建 Hashtable

Hashtable 对象包含用键/值对表示的项目。键被用作索引,通过搜索键,可以实现对值的快速搜索。

通过 Add() 方法向 Hashtable 添加项目。

数据绑定

Hashtable 对象可为下列的控件自动生成文本和值:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素):

然后添加创建列表的脚本,并且绑定列表中的值到 RadioButtonList 控件:

然后添加一个子例程,当用户点击 RadioButtonList 控件中的某个项目时,该子例程会被执行。当某个单选按钮被点击时,label 中会出现一行文本:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

12.ASP.NET Web Forms - SortedList 对象

SortedList 对象结合了 ArrayList 对象和 Hashtable 对象的特性。

SortedList 对象

SortedList 对象包含用键/值对表示的项目。SortedList 对象按照字母顺序或者数字顺序自动地对项目进行排序。

通过 Add() 方法向 SortedList 添加项目。通过 TrimToSize() 方法把 SortedList 调整为最终尺寸。

数据绑定

SortedList 对象可为下列的控件自动生成文本和值:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

13.ASP.NET Web Forms - XML 文件

可以绑定 XML 文件到列表控件。

一个名为 "countries.xml" 的 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<countries>

<country>
<text>Norway</text>
<value>N</value>
</country>

<country>
<text>Sweden</text>
<value>S</value>
</country>

<country>
<text>France</text>
<value>F</value>
</country>

<country>
<text>Italy</text>
<value>I</value>
</country>

</countries>

绑定 DataSet 到 List 控件

http://www.runoob.com/aspnet/aspnet-xml.html

14.ASP.NET Web Forms - Repeater 控件

Repeater 控件用于显示被绑定在该控件上的项目的重复列表。

绑定 DataSet 到 Repeater 控件

http://www.runoob.com/aspnet/aspnet-repeater.html

15.ASP.NET Web Forms - DataList 控件

DataList 控件,类似于 Repeater 控件,用于显示绑定在该控件上的项目的重复列表。不过,DataList 控件会默认地在数据项目上添加表格。

16.ASP.NET Web Forms - 数据库连接

17.ASP.NET Web Forms - 母版页

母版页为网站的其他页面提供模版。

母版页

母版页允许为web 应用程序中的所有页面(或页面组)创建一致的外观和行为。

母版页为其他页面提供模版,带有共享的布局和功能。母版页为内容定义了可被内容页覆盖的占位符。输出结果是母版页和内容页的组合。

内容页包含您想要显示的内容。

当用户请求内容页时,ASP.NET 会对页面进行合并以生成结合了母版页布局和内容页内容的输出。

母版页实例

<%@ Master %>

<html>
<body>
<h1>Standard Header From Masterpage</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>

上面的母版页是一个为其他页面设计的普通 HTML 模版页。

@ Master 指令定义它为一个母版页。

母版页为单独的内容包含占位标签 <asp:ContentPlaceHolder>

id="CPH1" 属性标识占位符,在相同母版页中允许多个占位符

这个母版页被保存为 "master1.master"

lamp 注释:母版页也能够包含代码,允许动态的内容。

内容页实例

<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>Individual Content</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</asp:Content>

上面的内容页是站点中独立的内容页中的一个。

@ Page 指令定义它为一个标准的内容页。

内容页包含内容标签 <asp:Content>,该标签引用了母版页(ContentPlaceHolderId="CPH1")。

这个内容页被保存为 "mypage1.aspx"

当用户请求该页面时,ASP.NET 就会将母版页与内容页进行合并。

注释:内容文本必须位于 <asp:Content> 标签内部。标签外的内容文本是不允许的。

带控件的内容页

<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>RUNOOB</h2>
<form runat="server">
<asp:TextBox id="textbox1" runat="server" />
<asp:Button id="button1" runat="server" text="Button" />
</form>
</asp:Content>

18.ASP.NET Web Forms - 导航

网站导航

ASP.NET 带有内建的导航控件。

 

猜你喜欢

转载自www.cnblogs.com/57rongjielong/p/9200494.html