谈谈Request和Response这两个对象的使用


本文摘自PHP中文网,作者零下一度,侵删。


ASP.NET对象有如下几个:

本文从“asp.net中通过from表单submit提交到后台的实例”来谈谈RequestResponse这两个对象的使用。

(一)引入实例

前台<body>中的表单代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<body>

    <form method="get" action="WebForm1.aspx">

        <table style="width:50%;">

            <tr>

                <td> </td>

                <td>

                    <input id="text1"  name="txtUserName" type="text" /></td>

                <td class="auto-style1"> </td>

            </tr>

            <tr>

                <td> </td>

                <td>

                    <input id="text2"  name="txtUserPwd" type="text" /></td>

                <td class="auto-style1"> </td>

            </tr>

            <tr>

                <td> </td>

                <td>

                    <input id="ccc" type="submit" value="提交" /></td>

                <td class="auto-style1"> </td>

            </tr>

        </table>

    </form>

</body>

表单中的method方法,即表单的提交方法。

表单中的action方法,指定表单的提交目标。

action=“WebFrom1”,指的是表单的提交后指向WebForm1窗体。在该路径的页面中,用Request.From可以接受到Post方法的数据。用Requet.QuestString可以接受Get的数据。具体用Post还是用Get,可以在表单中的Method属性中设置。

后台的C#代码:

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

public partial class WebForm1 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //Request三种获取表单值得方法。

 

        #region  对于post方法递交表单的获取值方法

        //string userName = Request.Form.Get("txtUserName").ToString();

        //string userPwd = Request.Form.Get("txtUserPwd").ToString();

        #endregion

 

        #region  对于get方法递交表单的获取值方法

        //string userName = Request.QueryString["txtUserName"].ToString(); 

        //string userPwd = Request.QueryString["txtUserPwd"].ToString();

        #endregion

        

        #region  对两者方法都适用的方法,运用Reuqest的索引值去获取所要求的表单值

        string userName = Request["txtUserName"].ToString();

        string userPwd = Request["txtUserPwd"].ToString();

        #endregion

        Response.Write("登陆的用户名为:" + userName + ";密码为:" + userPwd);

 

        if (userName=="a"&&userPwd=="b")

        {

            Response.Redirect("WebForm2.aspx");

        }

        else

        {

            Response.Redirect("login.html");

        }      

    }

}

(二)Request对象和Response对象用法总结

一、Request对象

Request三种获取表单值得方法的具体实现,我已都写入到后代代码的实例中了,在这里就不赘述。

这里需要注意的是:get和post方法的区别如下:

get方法提交,直接定义一个url就可以传值。缺点是,传的值是明码显示的。因为浏览器显示的字符是有长度的,所以他的数据显示的时候是受限制的。

post提交,是把数据作为一个整个集合进行提交,对于post方法传值的方法传的参数不会在url中用明码显示。

二、Response对象

response对象,最主要的用到的方法是respone.write(string)和responst.redirect(url).

response.write(string)的作用是从服务器端向客户端返回数据(写数据)。

response.rediec("url")的作用是在服务器端重定向另一个网页。

【相关推荐】

1. 总结Asp.net内置对象之Request对象使用实例

2. 分享一个Request对象小案例

3. 分享asp中request对象五个获取客户端资料的方法

4. 详解ASP.NET 系统对象之Request

以上就是谈谈Request和Response这两个对象的使用的详细内容!

相关阅读 >>

详解asp.net 系统对象之Request

总结asp.net内置对象(response)使用方法实例

system.threading.threadabortexception: 正在中止线程

分享asp中Request对象五个获取客户端资料的方法

谈谈Request和response这两个对象的使用

分享asp.net内置对象之response对象教程

asp.net在底层类库中获取session

总结asp.net内置对象之Request对象使用实例

asp.net从客户端中检测到有潜在危险的Request.form值

分享一个Request对象小案例

更多相关阅读请进入《Request》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...