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. Linux命令之ls

    ls命令 用处:列出此目录下的文件和目录 用法:直接在终端中输入ls就好了 示例: 看到了吗,文件夹和文件都显示出来了哦 可是,电脑里面有时会有一些隐藏文件,我还想看到这些隐藏文件怎么办呢?加  -a ...

  2. jQuery EasyUI Datagrid性能优化专题

    jQuery  EasyUI的Datagrid组件功能算是很强大了,不过性能确实不怎么乐观,而对于性能问题,网络上几乎也找不到相关的优化资料,所谓的牛人们可能都望而却步了.本博客以后会带着分析Data ...

  3. 《高性能Mysql》讲聚簇索引

    <高性能Mysql>原文 聚簇索引如下图为聚簇所有的存储方式,聚簇实际不是一种索引,而是一种数据的存储方式,InnoDB的聚簇事假在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇 ...

  4. C# Winform中慎用Application.DoEvents

    private void Add() { ; i < ; i++) { Button button = new Button(); button.Width = ; button.Height ...

  5. java 调用windows的COM组件举例(使用JACOB)

    java 调用windows的COM组件举例(使用JACOB) (转自这里) 最近公司需要做一个效果,开发一个程序能在程序运行时打开microsoft office的相关软件,实时写入,然后能关闭,你 ...

  6. 树的dfs序.欧拉序

    dfs序 ==先序,连续一段区间就是子树

  7. keepalived高可用系列~通用基础

    简介:今天咱们来聊聊keepalived一 keepalived 架构 1  标准架构: keepalived+lvs/haproxy+后端 real server(mysql从库,nginx.myc ...

  8. Application生命周期

    onCreate 在创建应用程序时创建 onTerminate 当终止应用程序对象时调用,不保证一定被调用,当程序是被内核终止以便为其他应用程序释放资源,那么将不会提醒,并且不调用应用程序的对象的on ...

  9. SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解

    1.@SpringBootTest单元测试实战 简介:讲解SpringBoot的单元测试 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> &l ...

  10. Mybatis进阶学习笔记——关系查询——一对一查询

    用户和订单的需求 通过查询订单,查询用户,就是一对一查询 (1)自定义JavaBean(常用,推荐使用) <select id="queryOrderUser" result ...