步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)
生活随笔
收集整理的這篇文章主要介紹了
步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
WebForm成功之處在于:實(shí)現(xiàn)的代碼后置,和asp相比實(shí)現(xiàn)了html代碼和C#代碼分離.但 aspx和aspx.cs之間的強(qiáng)耦合和性能方面(特別是服務(wù)器控件)做的不是很好.
參照步步為營(yíng)-68完成相同功能的小例子
1?實(shí)現(xiàn)自增
1.1?通過客戶端控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增.aspx.cs" Inherits="_01_小實(shí)例._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> aspx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace _01_小實(shí)例 {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;}}} } aspx.cs1.2?通過服務(wù)端控件實(shí)現(xiàn)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增(服務(wù)端控件).aspx.cs" Inherits="_01_小實(shí)例._01_自增_服務(wù)端控件_" %><!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> aspx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace _01_小實(shí)例 {public partial class _01_自增_服務(wù)端控件_ : 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();}} } aspx.cs2?實(shí)現(xiàn)加法計(jì)算器
2.1?通過客戶端控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法計(jì)算器.aspx.cs" Inherits="_01_小實(shí)例._02_加法計(jì)算器" %><!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> aspx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace _01_小實(shí)例 {public partial class _02_加法計(jì)算器 : 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;}} } aspx.cs2.2?通過服務(wù)端控件實(shí)現(xiàn)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法計(jì)算器(服務(wù)端控件).aspx.cs" Inherits="_01_小實(shí)例._02_加法計(jì)算器_服務(wù)端控件_" %><!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> aspx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _01_小實(shí)例 { public partial class _02_加法計(jì)算器_服務(wù)端控件_ : 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(); } } } aspx.cs3?div的自增長(zhǎng)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="03-div的增長(zhǎng).aspx.cs" Inherits="_01_小實(shí)例._03_div的增長(zhǎng)" %><!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="長(zhǎng)" /></form></div></body> </html> aspx using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace _01_小實(shí)例 {public partial class _03_div的增長(zhǎng) : 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;}} } aspx.cs轉(zhuǎn)載于:https://www.cnblogs.com/YK2012/p/7017526.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大白菜win8怎么上网 如何在win8上
- 下一篇: 怎么查看u盘序列号 查看U盘序列号的方法