2. 启始页
3. 创建第一个Web页面
在所有的语言学习起始都是通过实现一个Hello World来进行第一个样例的,我们学习ASP.NET也一样,我们也建立一个Hello World Web应用程序。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click Me"
onclick="Button1_Click" />
</div>
</form>
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("大家好,欢迎大家使有ASP.NET开发");
}
4. 项目和文件
|
文件类型
|
扩展名
|
|
页面
|
.aspx
|
|
用户控件
|
.ascx
|
|
Web服务
|
.asmx
|
|
母版页
|
.master
|
1. Web Form 的内容
AutoEventWireup="true"CodeBehind="Default.aspx.cs"
Inherits="WebApplication11._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>
<form id="form1" runat="server">
<div>
<asp:LabelID="Label1" runat="server" Text="Label">
<asp:TextBox ID="TextBox1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
2. Page页面指令
<%@ Page
Language="C#"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication11._Default" %>
|
属性
|
值
|
说明
|
|
Language
|
VB/C#/JS
|
内联代码所使用的编程语言
|
|
AutoEventWireup
|
True、false
|
启用或者禁用自动绑定页的事件处理方法。这些方法的命名规范为page_event
|
|
CodeBehind
|
文件名
|
VS2008所使用代码隐藏文件的名字
|
|
Inherits
|
类名
|
供页继承的代码隐藏类
|
3. Doctype类型
但是在XHTML中DOCTYPE声明是必不可缺少的,它告诉浏览器和确认服务,该文档遵循哪一个xhtml版本.如果你没有设定文档中的DOCTYPE 声明,那么所设计的文档就不能正常显示.DOCTYPE声明中的关键部分是DTD(Document Type Definition,文档类型定义),它告诉浏览器该文档遵循的是那一个xhtml版本,面目前xhtml有以下三个版本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
1. Code-Behind模式
2. 添加事件
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text = "Here is some sample text.";
3. 智能提示
Visual Studio提供的许多智能技术主要来源于IntelliSense。它提供了一种类似于微软Office一类所提供的语法检测技术。开发人员可以用代码编辑器来添加、格式化和编辑代码。
|
后缀
|
说明
|
|
.aspx
|
一个ASP.NET WEB应用程序页面,主要包括了一些页面控件、CSS文件和大部分表示层的东西,用户也主要是通过这个文件来访问ASP.NET应用程序
|
|
.ascx
|
一个ASP.NET用户控件,我们将再后面学习到
|
|
.asmx
|
一个WEB SERVICE页面请求,我们将再后面学习到
|
|
Web.config
|
ASP.NET WEB应用程序的配置文件
|
|
Global.asax
|
一个全局页面事件控件文件
|
|
.CS
|
代码后置文件,主要用于放置页面逻辑
|
|
目录
|
说明
|
|
Bin
|
包含了所有ASP.NET中的编译组件。
|
|
App_Code
|
包含了所有你需要动态编译的应用程序组件
|
|
App_GlobalResources
|
存储了WEB的全局资源文件,可以通过这个目录来实现多语言支持
|
|
App_LocalResources
|
其作用与App_GlobalResources作用有点类似,但主要是针对本地资源进行管理
|
|
App_Data
|
用于存储数据文件的目录
|
|
App_themes
|
用于存放样式模板文件
|
4. 转化HTML页面到ASP.NET页面
<html xmlns="http://www.w3.org/1999/xhtml">
<form method="post">
<input type="submit" value="OK" />
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs"
Inherits="CurrencyConverter" %>
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>转化程序</title>
<input type="text" ID="US" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server" />
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript" type="text/javascript">
function Convert_onclick()
{
var returnValue=document.getElementById("US").value;
document.write(returnValue*6.9);
}
<form id="form1" runat="server">
转换美元到人民币:<input id="US" type="text" /><br />
<input id="Convert" type="button" value="button"
onclick="return Convert_onclick()" />
<br />
5. View State简介
<html xmlns="http://www.w3.org/1999/xhtml">
<form id="form1" name="form1" method="post" action="CurrencyConverter.aspx">
<input type="hidden" id="__VIEWSTATE" name="__VIEWSTATE" value="dDw3NDg2NTI5MDg7Oz4=" />
转化:
<input type="text" id="US" name="US" />
美元到人民币
<br />
<br />
<input type="submit" value="OK" id="Convert" name="Convert" />
这个隐藏控件用加密与压缩格式中存储着这个页面中所有控件的状态,由于HTTP是一种无状态协议,为了保证页面能保存每一个控件的状态,所以就通过一种叫View State的技术保存住每一个控件运行状态的信息。
6. HTML控件类
一般情况下在不需要与后台进行交互时就使用HTML控件,它的占用的资源比较少,速度也比较快,在需要与后台进行交互时,就使用WEB控件,或者在HTML控件上加上runat="server" 也可以将一个HTML控件转换成交互控件,但这样占用的资源就和Web控件没任何分别了。
|
控件
|
相对应的HTML 标签
|
|
HtmlForm
|
<Form>
|
|
HtmlAnchor
|
<a>
|
|
HtmlTable
|
<table><tr><th><td>
|
|
HtmlInputButton
|
<input type=”button”> |
|
HtmlInputSubmit
|
<input type=”submit”> |
|
HtmlInputReset
|
<input type=”reset”> |
|
HtmlButton
|
<button>
|
|
HtmlInputCheckBox
|
<input type=”CheckBox”> |
|
HtmlInputRadioButton
|
<input type=”radio”> |
|
HtmlInputText
|
<input type=”text”> |
|
HtmlInputPassword
|
<input type=”password”> |
|
HtmlTextArea
|
<textarea>
|
|
HtmlInputImage
|
<input type=”image”> |
|
HtmlInputFile
|
<input type=”file”> |
|
HtmlInputHidden
|
<input type=”hidden”> |
asp.net之所以现在开发方便和快捷,关键是它有一组强大的控件库,包括web服务器控件,web用户控件,web自定义控件,html服务器控件和html控件等。这里我主要说说html控件、html服务器控件和web服务器控件的区别。
1 html控件:就是我们通常的说的html语言标记,这些语言标记在已往的静态页面和其他网页里存在,不能在服务器端控制的,只能在客户端通过javascript和vbscript等程序语言来控制。
2 html服务器控件:其实就是html控件的基础上加上runat="server"所构成的控件.它们的注意区别是运行方式不同,html控件运行在客户端,而html服务器控件是运行在服务器端的。微软官方资料是这样说的:
2、 隐藏客户端的不同,这样程序员可以把更多的精力放在业务上,而不用去考虑客户端的浏览器是ie还是firefox,或者是移动设备。
3、 Asp.net服务器控件可以保存状态到ViewState里,这样页面在从客户端回传到服务器端或者从服务器端下载到客户端的过程中都可以保存。
4、 事件处理模型不同,Html标注和Html服务器控件的事件处理都是在客户端的页面上,而Asp.net服务器控件则是在服务器上,举例来说:
<input id="Button4" type="button" value="button" runat="server"/>
是Html服务器控件,此时我们点击此按钮,页面不会回传到服务器端,原因是我们没有为其定义鼠标点击事件。
<input id="Btn4" type="button" value="button" runat="server" onserverclick="test" />
我们为Html服务器控件添加了一个onserverclick事件,点击此按钮页面会发回服务器端,并执行test(object sender, EventArgs e)方法。
<asp:Button ID="Button2" runat="server" Text="Button" />
|
属性
|
说明
|
|
IsPostBack
|
在ASP.NET使用Page.IsPostback,就可以避免往返行程上的额外工作:如果处理服务器控件回发,通常需要在第一次请求页时执行代码,该代码不同于激发事件时用于往返行程的代码。
如果检查Page.IsPostBack属性,则代码可按条件执行,具体取决于是否有对页的初始请求或对服务器控件事件的响应。这样做似乎很明显,但实际上可以忽略此项检查而不更改页的行为。
该属性用的好坏,直接关系到你程序运行是否按照你最初的意愿,也关系到整个页面的效率。因为,如果每次都会给控件绑定数据,不管你是第一次访问,还是提交了数据以后,那么这个页面程序的效率可想而知。
|
|
EnableViewState
|
在服务器端保存了网页各个控件及页面的状态,这其中包括各个控件在页面上的布局,和他们各自的属性。这些值就保存在ViewState下。
|
|
Application
|
