步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)
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>
<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实现一些简单实例)的更多相关文章
- 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
× 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...
- ASP.NET MVC 3 Model【通过一简单实例一步一步的介绍】
今天主要讲Model的两个方面: 1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍 2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model ...
- 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 500.23 ...
- ASP.NET中MD5的加密方式很简单
在ASP.NET中MD5的加密方式很简单,代码如下: FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5&quo ...
- 简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注 ...
- c++简单的ATL COM开发和调用实例(转)
c++简单的ATL COM开发和调用实例 1.打开VS2010,新建ATL COM 项目,步骤:“文件” -->“新建” -->“项目”,选择“Visual C++” -->“ATL ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- PHP分页初探 一个最简单的PHP分页代码的简单实现
PHP分页代码在各种程序开发中都是必须要用到的,在网站开发中更是必选的一项. 要想写出分页代码,首先你要理解SQL查询语句:select * from goods limit 2,7.PHP分页代码核 ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
随机推荐
- .NET MVC中的防CSRF攻击
一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...
- GsonWithoutObject 没有对象(脱离对象) 直接提取【转】
GsonWithoutObject 没有对象(脱离对象) 直接提取 ... gson json GsonWithoutObject 脱离对象, 直接提取 package temp; import to ...
- Java编程思想 学习笔记11
十一.持有对象 通常,程序总是根据运行时才知道的某些条件去创建新对象.在此之前,不会知道所需对象的数量,甚至不知道确切的类型. Java实用库还提供了一套相当完整的容器类来解决这个问题,其中基本的类 ...
- 解决iOS10下Meta设置user-scalable=no无效问题
苹果为了提高Safari中网站的辅助功能,屏蔽了Meta下的user-scalable=no功能 所以在iOS10下面,就算加上user-scalable=no,Safari浏览器也能支持手动缩放 解 ...
- 如何使用less(变量,混合,匹配,运算,嵌套...)
如何使用less及一些常用的(变量,混合,匹配,运算,嵌套...) less的介绍及编译工具 什么是less 1.LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法, ...
- 表格重新加载 where 携带上次值问题
表格重载两种方式: 方式一: tableIns.reload(options) 注意这种方式的重载是不会携带上次数据加载时的where值 //使用 第一次渲染返回的对象 var table ...
- WebAPI跨域处理
原文来自:http://www.cnblogs.com/heifengwll/p/6243374.html WebApi2跨域问题 一.跨域问题产生的原因:同源策略(Same origin pol ...
- u-boot移植(十二)---代码修改---支持DM9000网卡
一.准备工作 1.1 原理图 CONFIG_DM9000_BASE 片选信号是接在nGCS4引脚,若要确定网卡的基地址,则要根据片选信号的接口去确定. 在三星2440的DATASHEET中memory ...
- Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...
- EOF \n \0 NULL 之间的区别
\n 是换行符 \0 是字符串的结束标志 EOF是流的结束标志 FILE* 这种流 NULL 是指针为空 第一个问题是EOF 它是end of file的缩写,表示"文字流"(s ...