ASP.NET中div如何传值

在ASP.NET中,div如何传值呢?下面有一个简单的例子,先看下html代码:

<!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>
    <title></title>
</head>
<body>
    <form action="Handler1.ashx">
    <input type="hidden" name="isPostBack" value="true" />
    <div name="div1">
        @value
</div> <input type="submit" value="自增" /> </form> </body> </html>

现在需要实现单击“自增”按钮,使得div中的数字自增。服务器端的程序如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.IO;
 6 
 7 namespace WebApplication17
 8 {
 9     /// <summary>
10     /// $codebehindclassname$ 的摘要说明
11     /// </summary>
12    
13     public class Handler1 : IHttpHandler
14     {
15 
16         public void ProcessRequest(HttpContext context)
17         {
18             context.Response.ContentType = "text/html";
19             string ispostback = context.Request["isPostBack"];
20             string value ="0";
21             if (ispostback == "true") 
22             {
23                 value = context.Request["div1"];
24                 int i = Convert.ToInt32(value);
25                 i++;
26                 value = i.ToString();
27             }
28             string fullPath = context.Server.MapPath("HTMLPage1.htm");
29             string content = File.ReadAllText(fullPath);
30             content = content.Replace("@value", value);
31             context.Response.Write(content);
32         }
33 
34         public bool IsReusable
35         {
36             get
37             {
38                 return false;
39             }
40         }
41     }
42 }

当运行此服务器程序时,div中的数值消失了。回头看看浏览器中,所传入的只有ispostback=true并没有将div中的数值传入进去。这是为什么呢??
     在div中不存在一个叫做name的属性,传入数据时只认name,只有在input、select等中才会有name属性,所以div中的值无法传入进去。此时的问题应该如何解决呢??在html中有一个type为hidden的input,这里定义一个这样的input将其value值设置为div中的占位符@value,name就为div1。修改后的html代码如下:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7     <form action="Handler1.ashx">
 8     <input type="hidden" name="isPostBack" value="true" />
 9     <input type="hidden" name="div1" value="@value" />
10     <div>
11         @value</div>
12     <input type="submit" value="自增" />
13     </form>
14 </body>
15 </html>

所以此时传值传递的是hidden中的alue值。
修改后的服务端代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.IO;
 6 
 7 namespace WebApplication17
 8 {
 9     /// <summary>
10     /// $codebehindclassname$ 的摘要说明
11     /// </summary>
12    
13     public class Handler1 : IHttpHandler
14     {
15 
16         public void ProcessRequest(HttpContext context)
17         {
18             context.Response.ContentType = "text/html";
19             string ispostback = context.Request["isPostBack"];
20             string value ="0";
21             if (ispostback == "true") 
22             {
23                 value = context.Request["div1"];
24                 int i = Convert.ToInt32(value);
25                 i++;
26                 value = i.ToString();
27             }
28             string fullPath = context.Server.MapPath("HTMLPage1.htm");
29             string content = File.ReadAllText(fullPath);
30             content = content.Replace("@value", value);
31             context.Response.Write(content);
32         }
33 
34         public bool IsReusable
35         {
36             get
37             {
38                 return false;
39             }
40         }
41     }
42 }

运行后可以达到效果。通过以上比较可以得出,div中innerText进行传值可以通过隐藏字段进行传值,不能直接在div中使用name属性。实际上,服务端在执行请求时,所修改的@value实际上是隐藏字段的@value,当执行结束后再将此字段的值赋给div中的@value。

 

posted @ 2012-06-15 18:50  孙进  阅读(2294)  评论(0编辑  收藏  举报