教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net页面之间传值方法详解

asp.net页面之间传值方法详解

发布时间:2018-09-05   编辑:jiaochengji.com
教程集为您提供asp.net页面之间传值方法详解等资源,欢迎您收藏本站,我们将为您提供最新的asp.net页面之间传值方法详解资源
做程序的我想都会了解在开发时需要页面与页面之间传值以方便下一步要做什么,如在asp.net我们最常用的页面之间的传值就是get,post当然还有其它的一些不常用的在文章最后面我有简单介绍。

asp.net中页面之间传值我们用得最多的就是get,post这两种了,其它的如session,appliction,cookie等这些相对来说少用也不是常用的,只是在特殊情况下在使用了。

1. Get(即使用QueryString显式传递)
     方式:在url后面跟参数。
     特点:简单、方便。
     缺点:字符串长度最长为255个字符;数据泄漏在url中。
     适用数据:简单、少量、关键的数据。
     适用范围:传递给自己、传递给另一个目标页面;常用于2个页面间传递数据。
     用法:例如:url后加?UserID=…,跳转到目标页面,目标页面在伺服端可用Request.QueryString["InputText"]获取其指定参数值。

例子:(1)a.aspx 
 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5213')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5213>private void Button1_Click(object sender, System.EventArgs e)
{
  string s_url;
  s_url = "b.aspx?name=" Label1.Text;
  Response.Redirect(s_url);
}

(2)b.aspx 
 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9775')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9775>private void Page_Load(object sender, EventArgs e)
{
  Label2.Text = Request.QueryString["name"];
}

2. Post
     方式:通用的方式。利用form提交。
     特点:最常用的方法。常用技巧是把隐秘的数据存在隐藏域中由form提交。
     适用数据:大量数据,包括文件上传。
     适用范围:同Get方法
     用法:在客户端form指定action目标后submit、在asp.net的伺服端中使用server.Transfer(url)提交;在伺服端中用Request.Form["FormFieldID"]获取。

实例

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1589')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1589>

 <form id="form1" action="GetUserInfo.aspx" method="post">

      <table align="center"  cellpadding="0" cellspacing="10px" border="0" style="width: 320px;border:1px solid gray;" >
    <tr>
     <td colspan="2" class="title" align="center">修改密码</td>
    </tr>
    <tr>
     <td class="left">输入新密码:</td>
     <td class="right">
      <input type="password" id="userPwd" name="userPwd" style="width: 175px" />
     </td>
    </tr>
    <tr>
        <td class="left">&nbsp;&nbsp;确认密码:</td>
     <td class="right">
      <input type="password" id="verifyPwd" name="verifyPwd" style="width: 175px" />
     </td>
    </tr>
    <tr>
     <td colspan="2" style="text-align: center">
      <hr />
      <input type="hidden" name="username" value="<%=后台定义的变量读数据库时赋值%>"/></td>
      <input type="submit" value="修改" />
      <input type="reset" value="重置" />
     </td>
    </tr>
   </table>
 </form>


 有一个方法,根据POST和GET分别取值。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1448')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1448>

  public object string GetValueFromPage(string inputName,int type)
  {
            HttpContext rq = HttpContext.Current;
            object TempValue = "";

            if (type==1)
            {
                if (rq.Request.Form[inputName] != null)
                {
                    TempValue = rq.Request.Form[inputName];
                }

            }
            else if (type==0)
            {
                if (rq.Request.QueryString[inputName] != null)
                {
                    TempValue = rq.Request.QueryString[inputName];
                }
            }
            return TempValue;
}

那么当提交至GetUserInfo.aspx页面时,GetUserInfo.aspx.CS中可以这样获取值。


下面介绍一下其它的传值缺点和优点

cookie
     方式:将数据存在客户端的经典方法。
     缺点:安全性低、受客户端设置限制、一个站点仅存20个cookie,每个容量4096字节。

Session
     方式:将用户数据存储在伺服端。
     特点:asp.net中可以设置session的存储方式、位置、SessionID的保存是否依赖cookie。
             可以直接存储对象。
     缺点:asp.net中有失效的隐患
Cache
     方式:将用户数据存储在伺服端数据缓存中。
     特点:可以大大提高效率。 可以直接存储对象。
Appliction
     方式: 将数据存储于此,相当于全局变量。
     特点:可以直接存储对象。整个站点的共享数据
ViewState
     方式:asp.net特有机制,用来恢复页面状态。
     特点:将页面各控件及其所存数据序列化存在name为_ViewState的隐藏域中。
     缺点:存在HTML中,安全性较低。可以设置加密和验证,但数据量会大增、效率有影响。
Static
     方式: 将数据存于静态变量中。
     特点:利于提高效率。
     缺点:若用不好会致使用户或页面间数据紊乱,造成极大的隐患。建议只赋值一次,绝对禁止为单个用户而更改此值。


总结:了上面这些我没人详细介绍的页面之间传值方法看起来真不适合我们常用的WEB中的普通的页面与页面之间传值了,只能适合于特殊的场合了。

您可能感兴趣的文章:
asp.net 页面之间传值方法总结
.net C#中页面之间传值传参的六种方法
ASP.NET页面事件:顺序与回传方法详解
asp.net 页面传值的方法汇总
asp.net页面加载顺序细致解析
asp.net页面之间传值方法详解
.net入门教程:ASP.NET 在网页使用方法
php 页面之间传值实例教程
ASP.NET的跟踪模式详解
asp.net cookie详解

[关闭]
~ ~