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. Scala进阶之路-Scala中的Ordered--Ordering

    Scala进阶之路-Scala中的Ordered--Ordering 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   说道对象的比较,在Java中大家最熟悉不过的就是实现类本身实 ...

  2. 学习windows编程 day4 之 盯裆猫

    写着写着就困了.... 看这些测量数据就算了,是对各种函数的练习 #include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT ...

  3. sublime 将打字内容放在屏幕中央

    在settings user里添加一句: "scroll_past_end": true

  4. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()【转】【补】

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...

  5. CentOS6.8下Jenkins+maven+tomcat+git+shell自动构建、部署web应用环境的搭建

    参考资料:http://www.cnblogs.com/cheng95/p/6542036.html http://www.cnblogs.com/software-test/p/7068278.ht ...

  6. python番外篇--sql注入

    一.sql注入概念介绍 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意的)S ...

  7. STM32Cube自学-1

    为了方便调试,使用Keil5+Proteus.1.新建Proteus STM32项目,选择STMF103C6 2.新建STM32CubeMX项目,选择同型号CPU,选择UART1,Asynchrono ...

  8. nlogn LIS模板

    nlogn 模板 最长上升 #include<bits/stdc++.h> using namespace std; ; int n,x,y,a[N],num[N],d[N],len; / ...

  9. 20155332 2016-2017-2 《Java程序设计》第5周学习总结

    学号 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 1.Java中的所有不正常类都继承于Throwable类.Throwable主要包括两个大类,一个是Er ...

  10. cpp与其他语言相比较

    new  相当于 malloc ,delete 相当于  free ,用法一样 c++ 有 namespace,可以避免类名污染  namespace xx{} c++ 有类 这个与 c#.as3比较 ...