WebForm成功之处在于:实现的代码后置,和asp相比实现了html代码和C#代码分离.但 aspx和aspx.cs之间的强耦合和性能方面(特别是服务器控件)做的不是很好.

参照步步为营-68完成相同功能的小例子

1 实现自增

1.1 通过客户端控件

<%@ 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>

aspx

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;}}}
}

aspx.cs

1.2 通过服务端控件实现

<%@ 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>

aspx

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();}}
}

aspx.cs

2 实现加法计算器

2.1 通过客户端控件

<%@ 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>

aspx

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;}}
}

aspx.cs

2.2 通过服务端控件实现

<%@ 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>
&nbsp;<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_小实例 { 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(); } } }

aspx.cs

3 div的自增长

<%@ 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>

aspx

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;}}
}

aspx.cs

转载于:https://www.cnblogs.com/YK2012/p/7017526.html

步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)相关推荐

  1. Asp.net MVC 仿照博客园的简单网站首页 列表设计

    本来我打算采用ajax提交请求,异步的请求获取数据,但是我发现如果这样的话就会拖慢开发的进度,拖长时间.所以在这篇博客中仿照首页的列表设计其实和左侧列表网站分类采用了同样的方式,通过局部视图的方式呈现 ...

  2. asp.net获取页面url参数值的实现代码实例

    asp.net获取页面url参数值的实现代码实例 file: default.aspx.cs using system; using system.data; using system.configu ...

  3. python读文件代码-简单了解Python读取大文件代码实例

    这篇文章主要介绍了简单了解Python读取大文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通常对于大文件读取及处理,不可能直接加载到内 ...

  4. php注入类,简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注入类 ...

  5. java的简单工厂模式_java设计模式之简单工厂模式

    简单工厂模式的概念 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例. ...

  6. 设计模式----2(简单工厂模式的概念,简单工厂模式的实现,简单工厂模式的优缺点)

    简单工厂模式 简单工厂模式的概念 简单工厂模式属于类的创建型模式,又叫做静态工厂方法模式.通过专门定义一个类来负 责创建其他类的实例,被创建的实例通常都具有共同的父类. 具体分类 工厂(Creator ...

  7. Manage,管道的简单应用,进程池,队列的简单应用

    day37---Manage,管道的简单应用,进程池,队列的简单应用 今日内容: 1 生产者消费者模型 主要是为解耦 借助队列来实现生产者消费者模型 栈:先进后出(First In Last Out ...

  8. 用python画画简单代码_Python3使用PyQt5制作简单的画板/手写板实例

    1.前言 版本:Python3.6.1 + PyQt5 写一个程序的时候需要用到画板/手写板,只需要最简单的那种.原以为网上到处都是,结果找了好几天,都没有找到想要的结果. 网上的要么是非python ...

  9. yolo系列算法思想流程简单讲解概述————(究极简单的讲述和理解)

           在我想学习算法的时候,我看某些大佬特别喜欢上来就讲论文,给我搞的贼难受,毕竟本人太辣鸡了,上来这么搞看不懂,经过诸多算法的这样折磨.我打算根据自己的亲身经历和学习过程中遇到的问题出一期, ...

最新文章

  1. HDLBits 系列(17) 计数器的级联实现1000分频的分频器
  2. lk启动流程详细分析
  3. 选购一台计算机得出三条建议,如何挑选一台适合你的电脑?了解这几项配置即可!...
  4. 机器学习——人工神经网络之后向传播算法(BP算法)
  5. 每一个问题都是一把锁
  6. kobo glo刷安卓
  7. win10 你没有足够的权限执行此操作
  8. Html之 图像标记
  9. 什么是集体户口,优势、劣势
  10. 解决Class “xxx“ can not access a member of class “xxx“ with modifiers “private“
  11. 125页完整版智慧数据中台解决方案(附PPT全文)
  12. IceSSL插件配置
  13. 关于蜂产品保健的一些介绍
  14. struts、hibernate、spring这三个框架和J2EE是什么关系
  15. PAT甲级 1079(C++)
  16. 水塘采样(Reservoir sampling)算法
  17. 2022 Q2:『卖方金工』研报热度​榜单!
  18. c216芯片服务器内存,TD-S316E-S
  19. 从复变函数到傅里叶级数
  20. 山东大学计算机学院陈敏竹,我院学生辩论队荣获第十三届“山大杯”辩论赛冠军...

热门文章

  1. Win10如何取消开机密码
  2. Linux安装ipvsadm工具查看ipvs
  3. MyBatis在insert插入操作时返回主键ID
  4. MySQL使用覆盖索引来优化limit语句
  5. spring boot配置文件详解
  6. Quartz定时框架CronTrigger开发使用实例
  7. 自己实现strstr函数与strchr函数
  8. python(matplotlib5)——Contours 等高线图
  9. svn merger的时候 报远程主机强迫_SVN与Git比较的优缺点差异
  10. 5.3.1计算机网络传输层之TCP可靠传输