我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

ASP.NET 高级编程基础第二篇—练习说明

前言:这是我的第二篇高级编程博客,在这里我实现了几个小例子,而且里面用到了一些小技术,也就是给第一篇讲的内容做几个案例,数值的自动增长。

  1. 数值自增

(1) 我们要实现的是input的自增长,点击按钮,input文本框中的值就会自动增长。实现:新建IncValue1.ashx文件和IncValue1.htm文件,在这两个文件中书写代码。

1)IncValue1.htm

  

1 <form action="IncValue1.ashx">
2 
3         <input type="hidden" name="ispostback" value="true" />
4 
5         <input type="text" name="number" value="@value" />
6 
7         <input type="submit" value="自增长" />
8 
9     </form>

 

2)IncValue1.ashx

        

 1 context.Response.ContentType = "text/html";
 2 
 3         string ispostback = context.Request["ispostback"];
 4 
 5         string number = context.Request["number"]; //通过表单得到的数据都是string类型
 6 
 7         if (ispostback == "true") //说明是点击按钮进入的,需要把当前的数值自增长
 8 
 9         {
10 
11             int i = Convert.ToInt32(number);
12 
13             i++;
14 
15             number = i.ToString();
16 
17         }
18 
19         else //第一次进入值为0
20 
21         {
22 
23             number = "0";
24 
25         }
26 
27         string fullpath = context.Server.MapPath("IncValue1.htm");
28 
29         string content = System.IO.File.ReadAllText(fullpath);
30 
31         content = content.Replace("@value", number);
32 
33         context.Response.Write(content);
34 
35         context.Response.ContentType = "text/html";
36 
37         string ispostback = context.Request["ispostback"];
38 
39         string number = context.Request["number"]; //通过表单得到的数据都是string类型
40 
41         if (ispostback == "true") //说明是点击按钮进入的,需要把当前的数值自增长
42 
43         {
44 
45             int i = Convert.ToInt32(number);
46 
47             i++;
48 
49             number = i.ToString();
50 
51         }
52 
53         else //第一次进入值为0
54 
55         {
56 
57             number = "0";
58 
59         }
60 
61         string fullpath = context.Server.MapPath("IncValue1.htm");
62 
63         string content = System.IO.File.ReadAllText(fullpath);
64 
65         content = content.Replace("@value", number);
66 
67         context.Response.Write(content);

 

(2) 点评加法计算器的实现,实现:新建AddCalc1.ashx文件和AddCalc1.htm文件,在这两个文件中书写代码

1) AddCalc1.htm

   

 1 <form action="AddCalc1.ashx">
 2 
 3         <input type="hidden" name="hidden" value="yes" />
 4 
 5         <input type="text" name="num1" value="@num1" />+<input type="text" name="num2" value="@num2" />
 6 
 7         <input type="submit" value="=" />
 8 
 9         <input type="text" value="@result" readonly="readonly" />
10 
11     </form>

 

2) AddCalc1.ashx

 1 context.Response.ContentType = "text/html";
 2 
 3         string hidden = context.Request["hidden"];
 4 
 5         string result = "";
 6 
 7         string num1 = "";
 8 
 9         string num2 = "";
10 
11         if (hidden == "yes")
12 
13         {
14 
15             num1 = context.Request["num1"];
16 
17             num2 = context.Request["num2"];
18 
19             result = (Convert.ToInt32(num1) + Convert.ToInt32(num2)).ToString();
20 
21         }
22 
23         string fullpath = context.Server.MapPath("AddCalc1.htm");
24 
25         string content = System.IO.File.ReadAllText(fullpath);
26 
27         content = content.Replace("@num1", num1);
28 
29         content = content.Replace("@num2", num2);
30 
31         content = content.Replace("@result", result);
32 
33         context.Response.Write(content);

 

(3) Div版本的自增长,实现:新建IncValue2.htm和IncValue2.ashx文件

1) IncValue2.htm

   

 1 <form action="IncValue2.ashx">
 2 
 3         <input type="hidden" name="ispostback" value="true" />
 4 
 5         <input type="hidden" name="num1" value="@value" />
 6 
 7         <div>@value</div>
 8 
 9         <input type="submit" value="自增长" />
10 
11     </form>

2) IncValue2.ashx

 

 1   context.Response.ContentType = "text/html";
 2 
 3         string ispostback = context.Request["ispostback"];
 4 
 5         //为什么但是用div在服务器中取不出来值呢,因为不是服务器来读取客户的网页,而是浏览器收集客户在表单中输入的值,
 6 
 7         //然后形成请求参数发给服务器处理程序,由于没有把div当前的innerText发给服务器,所以服务器无法得知当前的值,
 8 
 9         //也不要幻想有办法能将div的innerText提交给服务器,因为只有设定了name的input,textare,select的value属性值才会被提交给服务器
10 
11         string value = "0";
12 
13         if (ispostback == "true")
14 
15         {
16 
17             value = context.Request["num1"];
18 
19             int i = Convert.ToInt32(value);
20 
21             i++;
22 
23             value = i.ToString();
24 
25         }
26 
27         string fullpath = context.Server.MapPath("IncValue2.htm");
28 
29         string content = System.IO.File.ReadAllText(fullpath);
30 
31         content = content.Replace("@value", value);
32 
33         context.Response.Write(content);

 

      注释:非表单元素无法将客户端的元素传递给服务器端,即使是表单元素也只能传递value值,对于其他属性值比如:背景颜色,大小等也都是无法传递的,因此对于这些值都要存在隐藏字段中,这就是asp.net中的viewState的实现原理。

posted @ 2012-08-08 13:35  Kencery  阅读(1205)  评论(6编辑  收藏  举报
友情链接:初心商城