1. 服务器端处理
虽然form.html能够通过post或者get方法产生回发,但是它仍然不能够处理用户输入,仍然是一个静态页面,现在我们需要在服务器端处理用户输入及回发。
在服务器端处理用户输入有很多方式,因为本文是ASP.NET教程,故只谈ASP.NET。
ASP.NET使用Web Form来描述Web页面,通过面向对象的编程方式,配合.NET Framework,使Web应用程序设计变得更简单,更高效,功能更为强大。
2. Web Forms
先从一个简单的示例程序开始:
doadd.aspx
HTML 代码:
<html>
<head>
<title>web form</title>
</head>
<body>
<form runat="server">
<asp:TextBox runat="server" id="value1" />
+ <asp:TextBox runat="server" id="value2" />
<asp:Button runat="server" Text=" = " OnClick="Add" />
<asp:Label runat="server" id="result" />
</form>
</body>
</html>
<script language="c#" runat="server">
void Add(object sender, EventArgs e)
{
if(value1.Text == "")
value1.Text = "0";
if(value2.Text == "")
value2.Text = "0";
try
{
result.Text = (int.Parse(value1.Text) + int.Parse(value2.Text)).ToString();
}
catch
{
result.Text = "Error";
}
}
</script>
这里假设读者已经正确安装IIS和ASP.NET,且Web服务器的主目录为C:\Inetpub\wwwroot\,以后如无特殊说明,均如此。
将该文件放在C:\Inetpub\wwwroot\,保存为doadd.aspx,然后在浏览器的地址栏输入http://localhost/doadd.aspx ,Web Form即呈现在浏览器中。
页面上有2个输入框,一个按钮,当用户单击“=”按钮时,程序会将2个输入框中的值相加,然后将结果显示在等号按钮之后,如果输入了错误的值,结果显示“Error”。
这个程序十分简单,但是它却是一个完整的动态Web程序。它在服务器端处理用户输入,然后动态生成HTML页面。
使用IE打开页面后,选择菜单【查看】|【源文件】,会看到下面的HTML源代码:
HTML 代码:
<html>
<head>
<title>web form</title>
</head>
<body>
<form name="_ctl0" method="post" action="1.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="dDwxOTE0NDY4ODE2Ozs+yeczcNbLHD6d7s7+Uue+wrxR/6I=" />
<input name="value1" type="text" id="value1" />
+ <input name="value2" type="text" id="value2" />
<input type="submit" name="_ctl1" value=" = " />
<span id="result"></span>
</form>
</body>
<html>
可以看出来TextBox控件变成了<input type=”text” />标记,Button控件变成了<input type=”submit” />标记,Label控件变了<span></span>标记,其中还有一个奇怪的<input type=”hidden”>标签,名为__VIEWSTATE,现在只需要知道它是ASP.NET用来处理服务器和浏览器间往返数据的一种机制,以后会详细介绍。
观察aspx文件,可以看到form标签和其中的所有控件都有一个属性:runat=”server”,这表示这个标签在服务器端运行,而不是直接发送到客户端浏览器。
一个<asp:...>控件实际上是对应于一个System.Web.UI.WebControls中的类,比如<asp:TextBox />对应于System.Web.UI.WebControls.TextBox,<asp:Button />对应于System.Web.UI.WebControls.Button,这些标签中还可以包含一些其他属性,例如<asp:Button/
虽然form.html能够通过post或者get方法产生回发,但是它仍然不能够处理用户输入,仍然是一个静态页面,现在我们需要在服务器端处理用户输入及回发。
在服务器端处理用户输入有很多方式,因为本文是ASP.NET教程,故只谈ASP.NET。
ASP.NET使用Web Form来描述Web页面,通过面向对象的编程方式,配合.NET Framework,使Web应用程序设计变得更简单,更高效,功能更为强大。
2. Web Forms
先从一个简单的示例程序开始:
doadd.aspx
HTML 代码:
<html>
<head>
<title>web form</title>
</head>
<body>
<form runat="server">
<asp:TextBox runat="server" id="value1" />
+ <asp:TextBox runat="server" id="value2" />
<asp:Button runat="server" Text=" = " OnClick="Add" />
<asp:Label runat="server" id="result" />
</form>
</body>
</html>
<script language="c#" runat="server">
void Add(object sender, EventArgs e)
{
if(value1.Text == "")
value1.Text = "0";
if(value2.Text == "")
value2.Text = "0";
try
{
result.Text = (int.Parse(value1.Text) + int.Parse(value2.Text)).ToString();
}
catch
{
result.Text = "Error";
}
}
</script>
这里假设读者已经正确安装IIS和ASP.NET,且Web服务器的主目录为C:\Inetpub\wwwroot\,以后如无特殊说明,均如此。
将该文件放在C:\Inetpub\wwwroot\,保存为doadd.aspx,然后在浏览器的地址栏输入http://localhost/doadd.aspx ,Web Form即呈现在浏览器中。
页面上有2个输入框,一个按钮,当用户单击“=”按钮时,程序会将2个输入框中的值相加,然后将结果显示在等号按钮之后,如果输入了错误的值,结果显示“Error”。
这个程序十分简单,但是它却是一个完整的动态Web程序。它在服务器端处理用户输入,然后动态生成HTML页面。
使用IE打开页面后,选择菜单【查看】|【源文件】,会看到下面的HTML源代码:
HTML 代码:
<html>
<head>
<title>web form</title>
</head>
<body>
<form name="_ctl0" method="post" action="1.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="dDwxOTE0NDY4ODE2Ozs+yeczcNbLHD6d7s7+Uue+wrxR/6I=" />
<input name="value1" type="text" id="value1" />
+ <input name="value2" type="text" id="value2" />
<input type="submit" name="_ctl1" value=" = " />
<span id="result"></span>
</form>
</body>
<html>
可以看出来TextBox控件变成了<input type=”text” />标记,Button控件变成了<input type=”submit” />标记,Label控件变了<span></span>标记,其中还有一个奇怪的<input type=”hidden”>标签,名为__VIEWSTATE,现在只需要知道它是ASP.NET用来处理服务器和浏览器间往返数据的一种机制,以后会详细介绍。
观察aspx文件,可以看到form标签和其中的所有控件都有一个属性:runat=”server”,这表示这个标签在服务器端运行,而不是直接发送到客户端浏览器。
一个<asp:...>控件实际上是对应于一个System.Web.UI.WebControls中的类,比如<asp:TextBox />对应于System.Web.UI.WebControls.TextBox,<asp:Button />对应于System.Web.UI.WebControls.Button,这些标签中还可以包含一些其他属性,例如<asp:Button/
| 对此文章发表了评论 |

