Summary of how to send value in ASP page

Source: Internet
Author: User
Tags html form

by learning ASP. NET, most of it is the use of the control, and the greatest difference in the use of the control is when the page is transmitted value, and C/s learning is very different. The video is more fragmented, and those who move to summarize.

Create a new Web Form form (Form1) in ASP., and submit the data in the form.

<span style= "FONT-SIZE:18PX;" ><form id= "Form1" method= "post/get" action = "WebForm1.aspx" runat= "Server" ></span>

Form1 The Default property method, which is the way to submit data, has two get and post. Typically, if there is no special designation, the default is post. The action is followed by the URL of the specified receive form, that is, after the user submits the data, the page is used to receive the display.    

1. Get and post

Get: Get. When we use it, when we pass the value, we use the QueryString property to return the result of the query to the client as a string.
The parameter is assigned to the URL in the browser address bar "? "Separate and pass the value. So the parameters to be passed are exposed to the user. It is conceivable that this is not safe.

Specific use:

Create a new Login.aspx (Start page), HTML code:

<span style= "FONT-SIZE:18PX;" ><form id= "Form1" method= "get" action = "WebForm1.aspx" runat= "Server" > <!--get, using </span><pre Name= "code" class= "HTML" >webform1.aspx page to receive Login.aspx submitted data-    <div>    user name: <asp:textbox id= " txtUserName "runat=" Server ></asp:TextBox> <!--add user name text box-    password: <asp:textbox id= "Txtpassword" runat= "Server" ></asp:TextBox> <!--fill in the password-        <asp:button id= "Button1" runat= "Server" text= "Submit"/ >    </div>    </form>
then, in the received form code, you need to state the specific data received, and the specific operation to be done, here using get method, so use the querstring this property to get.

As follows:code in WebForm1.aspx.cs

<span style= "color: #000000;" >string username = request.querystring["txtUserName"]; Gets the contents of the corresponding control, where the text box needs to be entered in the text box name string password = request.querystring["Txtpassword"]; Response.Write ("The user name you entered is:" + username + "The password you entered is" +password);</span>
after you run login. aspx, you will see the desired page in the WebForm1.aspx page.

Post: When passing a value, use Request.Form to get the data. When you pass a value, the parameter is not directly displayed in the browser's address bar, and is therefore relatively more secure.

Specific use:

HTML form code, and the control content and property names do not change. It still uses WebForm1 for reception.

<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>
here, the post get data writing format can also be:

<span style= "color: #000000;" >stringusername=request.form.get["txtUserName"]. ToString ();//Password Empathy </span>
or

<span style= "color: #000000;" >stringusername=request.form["txtUserName"]. ToString ();</span>
so through the small example above, after running, look at the results

Login.aspx page:


After submitting the page using the Get method:


Copy this address down:

Http://localhost:6186/WebForm1.aspx? __viewstate=f%2flyu9jjce%2bdvcxjs%2bc4w44xolwk0t6t8aek9sdfe% 2bql7ohruw51avmps0upch2tmb7hcnq8bcu8zpgkvancsdgoexksqq8uhzhdiyqvocq%3d&txtusername=123&txtpassword =123&button1=%e6%8f%90%e4%ba%a4&__viewstategenerator=c2ee9abb&__eventvalidation= c9fdalfhbwzc0ndmny0h2oeojh8ggwkg2l54ydj2g%2fzayafhpbh% 2ffozif5hht6fvwnmypnrjdfrj6trkkdvsgpbqyxhx72zdol2wb9oyujpzzojkvc7ikj2svo2phgxoygkyn7wjlkmsy5298laupx30anjlc% 2fy0fzrmwx5d6ni%3d

It can be found that the parameters it will pass are added to thebehind the destination URL.

The post-commit interface is used: for example, it is simple, and the address of the Start Page Login.aspx page is the same.


Note: In the small example here, take the Txtbox control as an example, when the value is passed the TextBox control name, and when we use the repeater or GridView data binding, when the value is passed, you can directly use the database field name to pass the value.

Summary: Through the above, probably understand the difference between get and post, when using the Get method to commit, involves a lot of URL value, the following about the URL of the page to turn several types.

URL Steering page

The use of URL value, mainly has

Response.Redirect ("url");

Server. Transfer

The way the two turn to the page reflects a similar function, can be directly to another page, but the transfer method can implement the object value, that is, in the submission page code can be declared an object and the specific properties (similar to the three layer of entity properties of a declaration), You can invoke the properties of the objects in the previous page in the Turn page.

Server.Execute: Not currently used, function: Server side to execute another page, control to return to the current page.

The Server.Execute method allows the current ASPX page to execute a specified ASPX page on the same Web server, and when the specified ASPX page finishes executing, the control flow returns to the original page where the Server.Execute call was made --a function equivalent to invoking the page itself


Some of the above is to implement page steering in your code, and you can also use HTML markup for example <a href= "url" ></a>

second, the public value of the way1, Application

Global variables for the entire application.

It is somewhat similar to a hash table when it is used for passing values. There are several ways to assign a value to a key value

application["Key"]=value; application["Key", value]; application["Key"]-----The Default object type, which is really storing objects

Typically, when using application, you need to lock it to prevent conflicts in the event of concurrent operations. as follows, make a call to it:

Application.Lock ();      Locking string a =convert. ToString (application["key"]); Specific Operation Application.UnLock ()//unlock

Defines the variable you want to declare in the Application_Start event when using application, ending in the end event

For example: Calculate the total number of Internet users and the current online population in the start event declaration, and can be connected with 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 number of people            application["online"] = 0;//        
In the end event, the following:

protected void Application_End (object sender, EventArgs e)        {            SqlConnection conn = new SqlConnection ("server=.; database=login;uid=sa;pwd=123456; ");            Conn. Open ();            Update database           SqlCommand cmd = new SqlCommand ("Update countpeople set num=" +application ["Total"]. ToString (), conn);            int count = cmd. ExecuteNonQuery ();            Conn. Close ();        }

2. Session Pass Value

session, so that the value passed in the specified time will always exist, but it is mainly for the user currently logged in, that is, for individuals, storage of user identity information. Also for the object type, when used, a cast is required. It is used similar to application. However, when used, the session can be removed and executed using the Remove method.

When the session is manipulated, it also begins with Session_Start, ending with end.

Then the above example, when the 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 access +1            application["online"] = (int) application["online" + 1;//number +1            application.unlock ();        
When the user is offline

protected void Session_End (object sender, EventArgs e)        {            application.lock ();            application["Online"] = (int) application["Online"]-1; Number of online -1            application.unlock ();        }

third, the cookie way

corresponding to the session, for the client user information storage method, temporarily did not contact, copied a piece of code to help understand. Need to use HttpCookie class before using

String LoginId = This.txtLogin.Text.Trim (); HttpCookie cookie = new HttpCookie ("UserName", LoginId); Two parameters one declares the bound field, and the other specific value Response.Cookies.Add (cookie);
You can also use jquery's value-transfer method, not yet learned to go deeper later.


Summary: The above is the video learned about the value-added method of ASP, in fact, there are many, such as , PreviousPageType, etc., can directly invoke the contents of the previous page, and so on. Preliminary study, a lot has not been practiced, first summed up all these, to be in depth later.

Summary of how to send value in ASP page

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.