Home > Backend Development > C#.Net Tutorial > Let's talk about the use of the two objects Request and Response

Let's talk about the use of the two objects Request and Response

零下一度
Release: 2018-05-26 10:32:49
Original
4387 people have browsed it


ASP.NET objects include the following:

This article starts from "## In #asp.net, we will talk about the use of the two objects Request and Response using the example of submitting to the background through the form submit in #asp.net.

(1) Introduction of examples

#                                                                                                 The form code in body>:

<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>
Copy after login
The method

method in the form is the form submission method.


The
action

method in the form specifies the submission target of the form.

Action="WebFrom1" refers to the form that points to the WebForm1 form after submission. In the page of this path, you can use Request.From to receive the data of the Post

method. Use Requet.QuestString to accept data from

Get. Whether to use Post or Get can be set in the Method attribute in the form. ##                                                                                                  Backend C# code:

    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");
            }       
        }
    }
Copy after login

(2) Request object and Response object Usage summary

1. Request object

## Request three methods to obtain form values The specific implementation has been written into the examples of future generations of code, so I will not go into details here. What needs to be noted here is:

The difference between get and post methods is as follows:

The get method is submitted, and a value can be passed by directly defining a url. The disadvantage is that the passed value is clearly displayed. Because the characters displayed by the browser have a length, the display of its data is limited. Post submission means submitting the data as a whole collection. The parameters passed by the value-passing method of the post method will not be displayed in clear code in the URL.

2. Response object

## The response object, the most important method used is response.write(string ) and response.redirect(url).

The function of response.write(string) is to return data (write data) from the server to the client.

The function of response.rediec("url") is to redirect another web page on the server side.

[Related recommendations]

1.

Summary of the usage examples of the Request object of Asp.net built-in objects

2. Share a small case of Request object3.

Share request in asp Object five methods to obtain client data4.

Detailed explanation of ASP.NET system object Request

The above is the detailed content of Let's talk about the use of the two objects Request and Response. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template