步步为营-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 ...
随机推荐
- 面向对象【day07】:类的属性(五)
本节内容 概述 公有属性 一.概述 前面我们讲了类的私有属性,现在我们来说说类的公有属性,这边很容易被人弄混淆,有人觉的,在__init__()构造方法中,除了私有属性,其他的都是公有属性了,其实这是 ...
- 条理清晰的搭建SSH环境之添加所需jar包
一.首先介绍要添加框架环境: JUnit Struts2 Hibernate Spring (1)配置JUnit /**-------------------------添加JUnit-------- ...
- python学习笔记8-邮件模块
我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下: impo ...
- DotNetBar ItemPanel添加WinForm Control
int itemcount = 20; Random rd = new Random(); itemPanel1.Items.Clear(); for (int i = 0; i < itemc ...
- 三、内存管理单元---MMU
3.1 MMU介绍 3.1.1 MMU 特性 内存管理单元(Memory Management Unit)简称MMU,它负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查.现在的多用户多进 ...
- codeforces724G Xor-matic Number of the Graph
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- C# cmd调用外部命令
void test2() { Process process = new Process(); //C:\\Users\\Administrator\\Desktop\\ffmpeg\\bin\\ff ...
- Mybatis入门学习笔记
1.定义别名 在sqlMapConfig.xml中,编写如下代码: <!-- 定义别名 --> <typeAliases> <!-- type: 需要映射的类型 alia ...
- CentOS 6.8 部署django项目一
CentOS 6.8 部署django项目二 1.安装python3.5(默认是2.6) 参考:http://blog.csdn.net/shaobingj126/article/details/50 ...
- ubuntu14.04 安装 CUDA 7.5 / CUDA 8.0
原文转自:http://blog.csdn.net/masa_fish/article/details/51882183 CUDA7.5和CUDA8.0的安装过程是一毛一样的.所以如果安装CUDA8. ...