.net中常用的几种页面间传递参数的方法

1.session方便,但容易丢失;

2.application全局;

3.cookie简单,但是可能不支持,容易被伪造;

4.input type="hide"隐藏域,简单,但容易被伪造

5.url参数简单,显示与地址栏,但是长度有限;

6.数据库稳定,安全,但性能较弱;

 

 

 

表单提交

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
传递页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head runat="server">
 
    <title>表单提交</title>
 
     <script type="text/javascript" language="javascript">
 
            function post()
 
            {     
 
                forPost.action="DestinationPage.aspx";       
 
                forPost.submit();
 
            }
 
    </script>
 
</head>
 
<body>
 
        <form id="forPost"  method="post">
 
         方式一:表单提交<br />
 
         <input type="text" id="SourceData2" runat="server"/>
 
         <input type="button" id="btnTransfer1" value="提交" onclick="post();" />            
 
        </form>  
 
</body>
 
</html>

 

1
2
3
4
5
6
7
8
9
10
接收页面代码
protected void Page_Load(object sender, EventArgs e)
 
{
 
    string a = Request.Form["SourceData2"].ToString();
 
    txt1.Value = a;             
 
}

 QueryString传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
传入页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head runat="server">
 
    <title>QueryString</title>
 
</head>
 
<body>
 
    <form id="form1" runat="server">
 
    <div>
 
        <input type="text" id="txt1" runat="server" />
 
        <input type="text" id="txt2" runat="server" />
 
        <input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
 
    </div>
 
    </form>
 
</body>
 
</html><br><br><br>protected void btn1_ServerClick(object sender, EventArgs e)
 
    {
 
        string aa = txt1.Value;
 
        string bb = txt2.Value;
 
        string url = "DestinationPage5.aspx?parameter1=" + aa + "&parameter2=" + bb;
 
        Response.Redirect(url, false);     
 
    }

 

1
2
接收页面代码
protected void Page_Load(object sender, EventArgs e)<br><br>    {<br><br>        txt1.Value = Request.QueryString["parameter1"].ToString();<br><br>        txt2.Value = Request.QueryString["parameter2"].ToString();<br><br>    }

 链接地址传值

1
<a href="DestinationPage6.aspx?param1=1111&param2=2222">跳转</a>

 

1
2
3
4
5
6
7
8
9
10
接收页面代码
protected void Page_Load(object sender, EventArgs e)
 
    {      
 
        txt1.Value = Request["param1"];
 
        txt2.Value = Request["param2"];
 
}

 Context传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head runat="server">
 
    <title>Context</title>
 
</head>
 
<body>
 
    <form id="form1" runat="server">
 
    <div>
 
        <input type="text" id="txt1" runat="server" />
 
        <input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
 
    </div>
 
    </form>
 
</body>
 
</html><br><br>protected void btn1_ServerClick(object sender, EventArgs e)<br><br>    {<br><br>        Context.Items["value"] = txt1.Value;<br><br>        Server.Transfer("DestinationPage3.aspx");      <br><br>    }<br>

 

1
2
3
4
5
6
7
8
9
10
接收代码
protected void Page_Load(object sender, EventArgs e)
 
    {
 
        string a = Context.Items["value"].ToString();
 
        txt1.Value = a;
 
    }

 Cookie传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
页面代码
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head runat="server">
 
    <title>Cookie</title>
 
</head>
 
<body>
 
    <form id="form1" runat="server">
 
    <div>     
 
        <input type="text" id="txt1" runat="server" />
 
        <input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
 
    </div>
 
    </form>
 
</body>
 
</html>
 
  
 
protected void btn1_ServerClick(object sender, EventArgs e)
 
    {
 
        string aa = txt1.Value;
 
        HttpCookie cookie = new HttpCookie("MyCookie", aa);
 
        Response.Cookies.Add(cookie);
 
        string url = "DestinationPage8.aspx";
 
        Response.Redirect(url, false);
 
    }

 

1
2
3
4
5
6
7
8
9
10
接收代码
protected void Page_Load(object sender, EventArgs e)
 
    {
 
        HttpCookie myCookie = Request.Cookies["MyCookie"];
 
        txt1.Value = myCookie.Value;
 
    }

 Session传值

通过Session取值,在一个页面中赋值,在其他页面中共享。为避免造成Session值的混乱无序,应尽量少用Session传非公共的变量。Session比较适合用来保存一些公共变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
页面
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head runat="server">
 
    <title>Session传值</title>
 
</head>
 
<body>
 
    <form runat="server">
 
        <input type="text" id="txt1" runat="server" />
 
        <input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
 
    </form>
 
</body>
 
</html>
 
  
 
protected void btn1_ServerClick(object sender, EventArgs e)
 
{     
 
    Session["value"] = txt1.Value;
 
    Response.Redirect("DestinationPage2.aspx");
 
}

 

1
2
3
4
5
6
7
8
接收值
protected void Page_Load(object sender, EventArgs e)
 
{
 
   txt1.Value = Session["value"].ToString();
 
}

Application传值

此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用于计数器等需要全局变量的地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
页面
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Application</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txt1" runat="server" />       
        <input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
    </div>
    </form>
</body>
</html>
 
protected void btn1_ServerClick(object sender, EventArgs e)
    {
        string aa = txt1.Value;
        Application["param1"] = aa;
        string url = "DestinationPage7.aspx";
        Response.Redirect(url, false);
    }

 

1
2
3
4
5
6
7
8
接收
protected void Page_Load(object sender, EventArgs e)
 
    {
 
        txt1.Value = Application["param1"].ToString();
 
    }

 

posted @   翘首以望  阅读(1510)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· 【译】.NET 升级助手现在支持升级到集中式包管理
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· Tinyfox 发生重大改版
点击右上角即可分享
微信分享提示