WebForm成功之处在于:实现的代码后置,和asp相比实现了html代码和C#代码分离.但 aspx和aspx.cs之间的强耦合和性能方面(特别是服务器控件)做的不是很好.
参照步步为营-68完成相同功能的小例子
1 实现自增
1.1 通过客户端控件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增.aspx.cs" Inherits="_01_小实例._01_自增" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" action="" method="post"> <input type="text" name="num" value="<%=Num %> "/> <input type="submit" value="自增" /> </form> </body> </html>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小实例 { public partial class _01_自增 : System.Web.UI.Page { public int Num { get; set; } protected void Page_Load(object sender, EventArgs e) { if (Request["num"]!= null) { int num = int.Parse(Request["num"]); num++; Num = num; } } } }
1.2 通过服务端控件实现
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增(服务端控件).aspx.cs" Inherits="_01_小实例._01_自增_服务端控件_" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtNum" runat="server">0</asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="自增" /> </div> </form> </body> </html>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小实例 { public partial class _01_自增_服务端控件_ : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { txtNum.Text = (Convert.ToInt32(txtNum.Text) + 1).ToString(); } } }
2 实现加法计算器
2.1 通过客户端控件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法计算器.aspx.cs" Inherits="_01_小实例._02_加法计算器" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" method="post" action=""> <input type="text" name="num1" value="<%=Num1 %>" /> + <input type="text" name="num2" value="<%=Num2 %>" /> <input type="submit" value="="/> <input type="text" name="result" value="<%=Result %>" /> </form> </body> </html>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小实例 { public partial class _02_加法计算器 : System.Web.UI.Page { public int Num1 { get; set; } public int Num2 { get; set; } public int Result { get; set; } protected void Page_Load(object sender, EventArgs e) { if (String.IsNullOrEmpty(Request["num1"]) || String.IsNullOrEmpty(Request["num2"])) { return; } int num1 = int.Parse(Request["num1"]); int num2 = int.Parse(Request["num2"]); int result = num1 + num2; Num1 = num1; Num2 = num2; Result = result; } } }
2.2 通过服务端控件实现
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法计算器(服务端控件).aspx.cs" Inherits="_01_小实例._02_加法计算器_服务端控件_" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="txtNum1" runat="server">0</asp:TextBox> +<asp:TextBox ID="txtNum2" runat="server">0</asp:TextBox> <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="=" /> <asp:TextBox ID="txtResult" runat="server">0</asp:TextBox> </form> </body> </html>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小实例 { public partial class _02_加法计算器_服务端控件_ : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { txtResult.Text = (int.Parse(txtNum1.Text) + int.Parse(txtNum2.Text)).ToString(); } } }
3 div的自增长
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="03-div的增长.aspx.cs" Inherits="_01_小实例._03_div的增长" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <div style="border:solid 1px red; width:<%=Len%>px;height:<%=Len%>px""> <form id="form1" method="post" action=""> <input type="hidden" name="len" value="<%=Len%>"/> <input type="submit" value="长" /> </form> </div> </body> </html>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小实例 { public partial class _03_div的增长 : System.Web.UI.Page { public int Len { get; set; } protected void Page_Load(object sender, EventArgs e) { int len ; if (!string.IsNullOrEmpty(Request["len"])) { len = Convert.ToInt32(Request["len"]) +10; }else{ len = 50; } Len = len; } } }