您的位置:首页 > 编程语言 > ASP

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种方法不错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐