ASP.NET页面间POST和GET传值

 ASP.NET页面间的传值有很多种方式,下面介绍一种最常见的传值方式,就是利用HttpContext传值。
分两种情况:
1、当表单提交的时候(POST)
页面1:userName.aspx
 <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">  确认密码:</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分别取值。

  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中可以这样获取值。

 string username=GetValueFromPage("username",1).ToString();//和页面表单的name对应
 string userPwd=GetValueFromPage("userPwd",1).ToString();
 string verifyPwd=GetValueFromPage("verifyPwd",1).ToString();

 2、GET传值
     如果是少数的值可以通过GET方式提交,直接写链接并把值附在后面就可以了。比如:

 <a href="GetUserInfo.aspx?username=<%=值%>&pwd=<%=值%>">传值</a>

 在GetUserInfo.aspx页面这样取:

  string username=GetValueFromPage("username",0).ToString();//和后面用&隔开的参数名对应
  string pwd=GetValueFromPage("pwd",0).ToString();

这样就能完成一些简单的传值操作。

你可能感兴趣的:(String,object,null,Class,asp.net,input)