在ASP.NET应用程序中,我们经常需要在页面间传递参数,经常使用的方法有如下几种:
1、用receive.aspx?id=5&code=3的形式将参数传到接收页面,然后在接收页面用Request.QueryString()方法得到参数值,这种最简单,但是有长度不能超过256K的限制
2、使用隐藏域<input name="code" type="hidden" >,将要需要传送的参数值赋给该隐藏域的Value,然后将该页面用Server.Transfer方法重定向到接收页面,在接收页面用Request.Form["code"]方法得到参数值,该方法可以突破256K的限制,但页面需重定向,隐藏域的值不是很安全。
3、使用Session,Application在页面间传参数,这样传递比较安全,但微软提供这些对象初衷不是用来传递页面参数的。
4、我自己的一种方法,具体思路,先将需要传递的参数组织成键值对存放在XML文件中,在用XMLHTTP将该文件发送到接收页面,在接收页面中解析该xml文件得到参数,这种方法不光光可以可以传递普通参数,还可以将对象系列化成XML文件后传到接收页面,而且整个过程可以做到发送页面不刷新,可以传送大对象,并且可以将传递的参数在接收页面进行处理,再将处理结果返回到发送页面,下面描述一下具体的实现过程:
我需要传送的XML文件如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
<?xml version="1.0"?>
2
<root>
3
<param key="0" value="value0" />
4
<param key="1" value="value1" />
5
<param key="2" value="value2" />
6
<param key="3" value="value3" />
7
<param key="4" value="value4" />
8
</root>
在发送页面组合该XML文件的代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
var xmlhttp=null;
2
if (window.XMLHttpRequest)
3
{
4
xmlhttp = new XMLHttpRequest();
5
}
6
if (!xmlhttp&&window.ActiveXObject)
7
{
8
try
9
{
10
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0")
11
}
12
catch(e)
13
{
14
try
15
{
16
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0")
17
}
18
catch(e)
19
{
20
try
21
{
22
new ActiveXObject("Msxml2.XMLHTTP")
23
}
24
catch(e)
25
{
26
try{new ActiveXObject("Microsoft.XMLHTTP")}
27
catch(e)
28
{}
29
}
30
}
31
}
32
}
33
function sendParameter()
34
{
35
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
36
xmldoc.loadXML('<?xml version="1.0"?><root/>');
37
for(var i=0;i<5;i++)
38
{
39
var node = xmldoc.createNode(1,"param","");
40
var attribID = xmldoc.createAttribute("key");
41
attribID.value = i;
42
node.setAttributeNode(attribID);
43
var attribIDCode = xmldoc.createAttribute("value");
44
attribIDCode.value = "value" + i;
45
node.setAttributeNode(attribIDCode);
46
xmldoc.documentElement.appendChild(node);
47
}
48
49
xmlhttp.open("POST","upload.aspx",true);
50
xmlhttp.onreadystatechange = rep
51
xmlhttp.send(xmldoc);
52
}
在接收页读取并解析XML文件
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
protected void Page_Load(object sender, EventArgs e)
2
{
3
if (!IsPostBack)
4
{
5
XmlDocument xmlDoc = new XmlDocument();
6
xmlDoc.Load(Request.InputStream);
7
XmlElement xmlElement = xmlDoc.DocumentElement;
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
10
{
11
}
12
}
13
}
参数传递完毕,大家发表一下意见吧!!
1、用receive.aspx?id=5&code=3的形式将参数传到接收页面,然后在接收页面用Request.QueryString()方法得到参数值,这种最简单,但是有长度不能超过256K的限制
2、使用隐藏域<input name="code" type="hidden" >,将要需要传送的参数值赋给该隐藏域的Value,然后将该页面用Server.Transfer方法重定向到接收页面,在接收页面用Request.Form["code"]方法得到参数值,该方法可以突破256K的限制,但页面需重定向,隐藏域的值不是很安全。
3、使用Session,Application在页面间传参数,这样传递比较安全,但微软提供这些对象初衷不是用来传递页面参数的。
4、我自己的一种方法,具体思路,先将需要传递的参数组织成键值对存放在XML文件中,在用XMLHTTP将该文件发送到接收页面,在接收页面中解析该xml文件得到参数,这种方法不光光可以可以传递普通参数,还可以将对象系列化成XML文件后传到接收页面,而且整个过程可以做到发送页面不刷新,可以传送大对象,并且可以将传递的参数在接收页面进行处理,再将处理结果返回到发送页面,下面描述一下具体的实现过程:
我需要传送的XML文件如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在接收页读取并解析XML文件
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)