asp.net简单显示学习笔记
2012-10-22 18:31
531 查看
实现一个如上图的功能的网页处理程序。
最简单的拖控件的。拖控件的代码就不贴了,如果哪天这个方式都忘了,真的完全没学啊。。。。。
1.用一般处理程序解决:
Count.ashx:
<%@ WebHandler Language="C#" Class="count" %>
using System;
using System.Web;
public class count : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/html";
int x = 0;
int y = 0;
int z = 0;
int k = 0;
int total = 0;
//int strA=Convert.ToInt32(context.Request.Form["stuA"]);
// int strA, strB, strC, strD, total;
// strA = strA = strC = strD = 0;
if (!string.IsNullOrEmpty(context.Request.Form["IsPostBack"]))
{
string num1 = context.Request.Form["stuA"];
string num2 = context.Request.Form["stuB"];
string num3 = context.Request.Form["stuC"];
string num4= context.Request.Form["stuD"];
if (!string.IsNullOrEmpty(num1) && !string.IsNullOrEmpty(num2) && !string.IsNullOrEmpty(num3) && !string.IsNullOrEmpty(num4))
{
if (int.TryParse(num1, out x) && int.TryParse(num2, out y) && int.TryParse(num3, out z) && int.TryParse(num4, out k))
{
total=x+y+z+k;
}
}
}
System.Text.StringBuilder countStu = new System.Text.StringBuilder();//构造函数
countStu.Append("<html><head><title>简单的统计学生人数</title></head><body><form action='' method='post'>");
countStu.Append(" 学校学生人数统计");
countStu.Append("<br/>A班学生人数 <input type='text' name='stuA' value='" + x.ToString() + "'><br/>");
countStu.Append("<br/>B班学生人数 <input type='text' name='stuB' value='" + y.ToString() + "'><br/>");
countStu.Append("<br/>C班学生人数 <input type='text' name='stuC' value='" + z.ToString() + "'><br/>");
countStu.Append("<br/>D班学生人数 <input type='text' name='stuD' value='" + k.ToString() + "'><br/>");
//countStu.Append("<input type='hidden' value='1' name='IsPostBack'>");
countStu.Append("<input type='submit' value='计算人数'>");
countStu.Append(" " + total.ToString());
countStu.Append("<input type='hidden' name='IsPostBack' value='1'>" +"</form></body></html>");
context.Response.Write(countStu.ToString());
}
public bool IsReusable {
get {
return false;
}
}
}2.老师教的方法,前台用html,减轻服务器负担。
。aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="Weblxt02fkj._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="600px" >
<tr>
<td colspan="2" align ="center" >
学校学生人数统计
</td>
</tr>
<tr>
<td width="300px">
A班学生人数
</td>
<td>
<input type ="text" id ="text1" name="r1" />
</td>
</tr>
<tr>
<td>
B班学生人数
</td>
<td>
<input type ="text" id ="text2" name="r2" />
</td>
</tr>
<tr>
<td>
C班学生人数
</td>
<td>
<input type ="text" id ="text3" name="r3" />
</td>
</tr>
<tr>
<td>
D班学生人数
</td>
<td>
<input type ="text" id ="text4" name="r4" />
</td>
</tr>
</table>
<br />
<input type="submit" id="Button" value="计算总数" />
<br />
<%= getStr()%>
</div>
</form>
</body>
</html>
后台处理文件。aspx。cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Weblxt02fkj
{
public partial class _Default : System.Web.UI.Page
{
private string str = "";
protected void Page_Load(object sender, EventArgs e)
{
string text1 = Request.Form["r1"];
string text2 = Request.Form["r2"];
string text3 = Request.Form["r3"];
string text4 = Request.Form["r4"];
int A = Convert.ToInt32(text1);
int B = Convert.ToInt32(text2);
int C = Convert.ToInt32(text3);
int D = Convert.ToInt32(text4);
string num = Convert.ToString(A+B+C+D);
if (text1 != null && text1 != "")
{
Button_Click(num );
}
}
public string getStr()
{
return str;
}
protected void Button_Click(string num)
{
str = "总人数为:" + num ;
}
}
}
真心觉得第2种方法不错。
相关文章推荐
- Sharepoint学习笔记—Debug&TroubleShooting--Developer Dashboard的使用(4.显示ASP.NET Tracing信息)
- ASP.NET网络安全简单防护公开课-视频学习笔记
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- ASP.NET,flexpaper,SWFTools 实现简单的PDF显示(三)
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET路由(一)
- asp.net mvc 4 高级编程学习笔记:第三章 视图(2)
- (5)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 熔断降级(Polly)
- asp.net自定义控件代码学习笔记
- ASP.Net MVC开发基础学习笔记(1):走向MVC模式
- (9)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- JWT算法
- .NET 框架类在asp.net 中的应用(一)学习笔记
- 【ASP.NET MVC 学习笔记】- 07 使用 Entity Framework
- 《Pro ASP.NET MVC 3 Framework》学习笔记之二十二【Controllers和Actions】
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET路由(二)
- 《ASP.NET MVC 4 实战》学习笔记 1:第一个MVC用程序(上)
- Asp.net Ajax 学习笔记5 UpdatePanel的使用(下)
- 【ASP.NET MVC 学习笔记】- 14 HtmlHlper的扩展方法
- 蛙蛙推荐:ASP.NET MVC学习笔记
- Pro ASP.NET MVC 5 Framework.学习笔记.6.3.MVC的必备工具
- ASP.NET 路由设置 学习笔记