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"></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) + ).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"></asp:TextBox>
+<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="=" />
<asp:TextBox ID="txtResult" runat="server"></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"]) +;
}else{
len = ;
}
Len = len;
}
}
}

aspx.cs

步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)的更多相关文章

  1. 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

      ×   检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...

  2. ASP.NET MVC 3 Model【通过一简单实例一步一步的介绍】

    今天主要讲Model的两个方面: 1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍 2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model ...

  3. 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 500.23 ...

  4. ASP.NET中MD5的加密方式很简单

    在ASP.NET中MD5的加密方式很简单,代码如下: FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5&quo ...

  5. 简单实用的PHP防注入类实例

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

  6. c++简单的ATL COM开发和调用实例(转)

    c++简单的ATL COM开发和调用实例 1.打开VS2010,新建ATL COM 项目,步骤:“文件” -->“新建” -->“项目”,选择“Visual C++” -->“ATL ...

  7. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  8. PHP分页初探 一个最简单的PHP分页代码的简单实现

    PHP分页代码在各种程序开发中都是必须要用到的,在网站开发中更是必选的一项. 要想写出分页代码,首先你要理解SQL查询语句:select * from goods limit 2,7.PHP分页代码核 ...

  9. Asp.net读取和写入txt文件方法(实例)!

    Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...

随机推荐

  1. Vue.js 模板指令

    1. 数据渲染:v-text.v-html(保存了html结构).{{}}(自动更新): 2. 控制模块隐藏:v-if:直接不渲染 DOM 元素: v-show:css 里的 display:none ...

  2. javascript Date定义和体验

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. weblogic11g 修改密码和重置密码【原】

    修改密码 知道密码的情况下,可参考该链接 http://www.cnblogs.com/may12138/p/6022946.html 或 http://www.cnblogs.com/lsdb/p/ ...

  4. 【JUC】JDK1.8源码分析之ReentrantReadWriteLock

    重入锁ReentrantLock是排他锁,排他锁在同一时刻仅有一个线程可以进行访问,但是在大多数场景下,大部分时间都是提供读服务,而写服务占有的时间较少.然而读服务不存在数据竞争问题,如果一个线程在读 ...

  5. web中的乱码处理

    1 .web中的中文乱码处理 1.页面设置pageEncoding="UTF-8" <%@ page contentType="text/html;charset= ...

  6. Jena搭建SPARQL查询RDF数据

    1 Jena搭建SPARQL查询RDF数据 1.1 Jena概要 · SPARQL是W3C的RDF数据工作组设计的一种查询语言和协议,用于RDF数据的查询.经过类似于JDK安装时候的配置,可以在命令行 ...

  7. JDBC preparedStatement分页和统计,批处理和事务

    一个类:DriverManager 四个接口:Connection.PreparedStatement .ResultSet.Statement 连接不上数据库出错的原因 1.数据库监听服务的配置不正 ...

  8. 寻路优化(二)——二维地图上theta*算法的设计探索

    这篇文章是基于上一篇文章的研究上进行的,使得路径更加的平滑和自然,特此记录.有错误欢迎大家批评指正.如需转载请注明出处,http://www.cnblogs.com/Leonhard-/p/68660 ...

  9. day1 查看当前目录命令:pwd

    用到查看当前目录的完整路径使用:pwd 物理路径和连接路径什么鬼?没明白暂时借鉴别人的记录下 显示当前目录的物理路径 pwd –P 1: [root@DB-Server init.d]# cd /et ...

  10. Remaiten-一个以路由器和IoT设备为目标的Linux bot

    Remaiten-一个以路由器和IoT设备为目标的Linux bot ESET的研究人员正在积极地检测以嵌入式系统为攻击目标的木马,受影响的有路由器,网关和无线访问点.近期,我们已经发现了一个相关的b ...