Summary of Asp. Net page value passing method, asp.net page summary

Source: Internet
Author: User
Tags asp net

Summary of Asp. Net page value passing method, asp.net page summary
Through learning asp.net, the most important thing to talk about is the use of controls. The biggest difference in the use of controls is that when values are transferred to pages, they are very different from C/S learning. The video is fragmented. Let's summarize those.

Create a new web form Form (form1) in asp.net and submit data in the form.

<span style="font-size:18px;"><form id="form1" method="post/get" action ="WebForm1.aspx" runat="server"></span>

The default form1 attribute method is the data submission method. There are two methods: get and post. Generally, if no special parameter is specified, the default value is post.Action is followed by the URL of the specified receiving form. That is, after the user submits the data, the page is used to receive the display.

1. get and post

Get: get. When using this function, you can use the querystring attribute to return the query result to the client as a string.
In the address bar of the browser, the parameter is assigned to the url with "?" Separate to pass values. Therefore, the parameters to be passed are exposed to the user. As you can imagine, this is not safe.

Specific use:

Create a login. aspx (start page) with HTML code:

<Span style = "font-size: 18px;"> <form id = "form1" method = "get" action = "WebForm1.aspx" runat = "server"> <! -- Get, use the </span> <pre name = "code" class = "html"> WebForm1.aspx page to receive login. data submitted by aspx --> <div> User name: <asp: TextBox ID = "txtUserName" runat = "server"> </asp: TextBox> <! -- Add username text box --> password: <asp: TextBox ID = "txtPassword" runat = "server"> </asp: TextBox> <! -- Enter the password --> <asp: Button ID = "Button1" runat = "server" Text = "Submit"/> </div> </form>
In the received form code, you need to specify the specific data to be received and the specific operation to be performed. Here, you can use the get method, so you can use the QuerString attribute to obtain the data. 

The code in WebForm1.aspx. cs is as follows:

<Span style = "color: #000000;"> string username = Request. queryString ["txtUserName"]; // obtain the content of the corresponding control. In the text box, enter name string password = Request. queryString ["txtPassword"]; Response. write ("the user name you entered is:" + username + "the password you entered is" + password); </span>
After running login. aspx, you can see the desired page on the webform1.aspx page.

Post: When passing values, use request. form to obtain data. When passing values, parameters are not directly displayed in the address bar of the browser, so they are relatively safer.

Specific use:

HTML form code. The control content and attribute name remain unchanged. WebForm1 is still used for receiving

<span style="font-size:18px;color:#000000;"><form id="form1" method="post" action ="WebForm1.aspx" runat="server"></span>
The code in WebForm1.aspx. cs is as follows:

<Span style = "color: #000000;"> string username = Request ["txtUserName"]. toString (); string password = Request ["txtPassword"]. toString (); Response. write ("the user name you entered is:" + username + "the password you entered is" + password); </span>
In this case, the format for retrieving data from post can also be:

<Span style = "color: #000000;"> stringusername = Request. Form. Get ["txtUserName"]. ToString (); // the same password </span>
Or

<span style="color:#000000;">stringusername=Request.Form["txtUserName"].ToString();</span>
Then, let's take a look at the result after running the example above.

Login. aspx page:


Pages submitted using the get method:


Copy the address:

Http: // localhost: 6186/WebForm1.aspx? _ VIEWSTATE = F % 2FLYU9jJCE % 2 BdVcxJs % signature % 3D & txtUserName = 123 & txtPassword = 123 & Button1 = % E6 % 8F % 90% E4 % BA % A4 &__ VIEWSTATEGENERATOR = C2EE9ABB & __ EVENTVALIDATION = C9fdalFhBwZC0ndMny0h2oEoJH8ggWKG2L54ydj2g % 2 FzAYAfHpbH % %%2fy0fzrmwx5d6ni % 3D

It can be found that the parameter to be passed is added after the destination URL.

The interface after post submission is used: for example, it is very simple, and the address of the login. aspx page on the start page is the same.


Note: In this example, the txtbox control is used as an example. The textbox Control name is used for value transfer. When we bind data using repeater or gridview, you can directly use the field names in the database to transmit values.

Summary: Through the above, we will probably understand the difference between get and post. When submitting using the get method, it involves a lot of URL transfer values. Below are several types of URL page redirection.

URL redirection page

Using URL to pass values mainly includes

Response. redirect ("url ");

Server. Transfer

The conversion method of the two to the page shows similar functions, both of which can be directly directed to another page, but the transfer method can realize object Value passing, that is, you can declare an object and a specific attribute in the submitted Page code (similar to a declaration of a layer-3 entity attribute). You can call the attributes of the object on the previous page on the page.

Server.exe cute: it has never been used yet. function: the server executes another page and the control returns to the current page.

Server. the Execute method allows the current ASPX page to Execute a specified ASPX page on the same Web Server. When the specified ASPX page is executed, the control process returns to the original page to issue the Server. execute call location -- equivalent to calling a function on its own page


The above are page turns in the code, and HTML tags can also be used for implementation, such as <a href = "url"> </a>

Ii. Public value transfer method 1. Application

Global variables for the entire application.

It is similar to a hash table when it is used for value passing. Assign a value to a key value in the following ways:

Application ["key"] = value; Application ["key", value]; Application ["key"] ----- default object type, which stores objects

When using an application, You need to lock it to prevent conflicts during concurrent operations. Call it as follows:

Application. lock (); // lock string a = convert. ToString (Application ["key"]); ...... specific operation Application. unlock () // unlock

Define the variables you want to declare in the Application_Start event when using application, and end in the end event.

For example, you can declare the total number of Internet users and the current number of online users in the start event and connect to the database.

Protected void Application_Start (object sender, EventArgs e) {SqlConnection conn = new SqlConnection ("server = .; database = login; uid = sa; pwd = 123456; "); conn. open (); SqlCommand cmd = new SqlCommand ("select * from countPeople", conn); int count = Convert. toInt32 (cmd. executeScalar (); conn. close (); Application ["total"] = count; // total Application ["online"] = 0; // number of online users}
The end event is as follows:

Protected void Application_End (object sender, EventArgs e) {SqlConnection conn = new SqlConnection ("server = .; database = login; uid = sa; pwd = 123456; "); conn. open (); // update the database SqlCommand cmd = new SqlCommand ("update countPeople set num =" + Application ["total"]. toString (), conn); int count = cmd. executeNonQuery (); conn. close ();}

2. Pass the session Value

Session, so that the transmitted value always exists within the specified time, but it is mainly for the current login user, that is, for the individual, and stores the user's identity information. It is also of the object type, which must be forcibly converted during use. It is similar to Application. However, the session can be removed and executed using the remove Method.

When you operate a session, it also starts with session_Start and ends with end.

The preceding example shows that when a user logs on to the page

Protected void Session_Start (object sender, EventArgs e) {Session. timeout = 1; Application. lock (); Application ["total"] = (int) Application ["total"] + 1; // total number of Internet users + 1 Application ["online"] = (int) application ["online"] + 1; // number of online users + 1 Application. unLock ();}
When a user goes offline

Protected void Session_End (object sender, EventArgs e) {Application. lock (); Application ["online"] = (int) Application ["online"]-1; // number of online users-1 Application. unLock ();}

Iii. cookie Method

It corresponds to the session, which is used to store user information on the client. It has not been accessed yet and has copied a piece of code to help you understand it. Httpcookie class is required before use

String LoginId = this.txt Login. text. trim (); HttpCookie cookie = new HttpCookie ("UserName", LoginId); // two parameters: one declaring the bound field, and the other specific value Response. cookies. add (cookie );
You can also use the value passing method of JQuery, which has not yet been learned and will be further explored.


Summary: there are many methods for transferring values related to asp.net learned in the video. For example, PreviousPageType can directly call the content of the previous page. A lot of preliminary studies have not yet been put into practice. Let's summarize them first. Please wait for a while.


How do I list the methods for passing values between ASP NET pages?

1. Use QueryString variable
QueryString is a simple method for transferring values. It can display the transmitted values in the address bar of a browser. This method can be used to transmit one or more numeric values with low security requirements or simple structure. However, this method cannot be used to pass arrays or objects. The following is an example:
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
String s_url;
S_url = "B. aspx? Name = "+ Label1.Text;
Response. Redirect (s_url );
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
Label2.Text = Request. QueryString ["name"];
}

2. Use the Application object variable
The scope of the Application object is global, that is, it is valid for all users. The common methods are Lock and UnLock.
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Application ["name"] = Label1.Text;
Server. Transfer ("B. aspx ");
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Application. Lock ();
Name = Application ["name"]. ToString ();
Application. UnLock ();
}

3. Use Session Variables
Presumably, this is definitely the most common usage. Its operations are similar to those of the Application, which act on individual users. Therefore, excessive storage will result in the depletion of server memory resources.
A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
Session ["name"] = Label. Text;
}

B. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
String name;
Name = Session ["name"]. ToString ();
}

4. Use Cookie object variables
This is also a common method. Like Session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request.

A. aspx C # code
Private void button#click (object sender, System. EventArgs e)
{
HttpCookie cookie_name = new HttpCookie (& qu ...... remaining full text>

How to pass values between pages in ASPNET

You can use the Gridview method!
Click the gridview task in the upper-right corner of the GridView.-edit the column-hyperlinkfield.
Then you can set its attribute DataNavigateUrlFilelds: primary key name;
DataNavigateUrFormat; Deatil. aspx? Primary Key name = {0 };
Don't forget, set the primary key name of datakeyNames in the GridView: OK,
Finally, on the Details. aspx page, string id = Reuqest ["primary key name"]. tostring ();
You will do anything else, huh, huh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.