Asp.net Summary (II. Details)

Source: Internet
Author: User
Overview

For the theoretical knowledge of this blog, you can refer to the previous blog, which will not be repeated here.



User Control and master page

The user control provides a small range of style control and more flexible code reuse. The master page provides style control and code reuse for the entire page.

Create a user control

Right-click the project name and choose "add"> "new item"> "Web user control". By combining common controls, you can compile the corresponding event Execution code to create the user control.

Use of User Controls

You can directly drag and drop the user control on the Web page to use it. The web page obtains the attributes of the user control and adds the corresponding attributes to the Custom User control. Then, you can access the Web page. The details are as follows:

Attributes added to the user control class

Public String age {// read-only and write-only can be written to one of them. // The read attribute get {return textbox1. text;} // write property set {textbox1. TEXT = value ;}}

Web page usage

Protected void button1_click (Object sender, eventargs e) {webusercontrol11.age = "Hello! "; // Webusercontroll1 is the ID of the user control}

Dashboard page creation

Right-click the project name and choose "add"> "new item"> "master page". You only need to set the same settings as the web page. However, you need to say: <asp: contentplaceholder id = "contentplaceholder1" runat = "server"> This is a placeholder, which is a place for the website that uses the template page, you cannot set the corresponding controls on the template page </ASP: contentplaceholder>. Placeholders are automatically added to vs 2010, but must be written manually in the child-parent version.

Create a Web page that applies the master page

Right-click the project name and choose "add"> "new item"> "use master page"> "select the corresponding master". When creating a parent version, select the nested parent version page.

The following is a comparison of the interface design code between the master page and the web page that references the master page.

Master


Reference the web page of the upper dashboard


The code for obtaining the control attributes on the master page is as follows:

Calendar ca = (Calendar)this.Master.FindControl("Calendar1");

The above code can access the control attributes in the master. Note that if the web page uses the child-parent version, you do not know how to access the corresponding control.


Response object

Outputs Data to the client, including outputting data to the browser, redirecting the browser to another URL, or outputting a cookie file to the browser.

Output specific data

Response. Write ("display in Browser ");

Output data containing a special string set

Response. write ("<a href=default.htm> data with HTML tags

After receiving the string, the browser immediately interprets the string and displays the result on the browser.

Output file

Response. writefile ("file.txt ");

Write all the content of the text file directly to the output and send it to the browser.

Turn the webpage to (output the entire static page)

Select Case txtName.Text           Case "adimn"                    Response.Redirect("ManagePage.aspx?uid=" + txtName.Text + "&pwd=" + txtPwd.Text)           Case "teacher"                     Response.Redirect("TeacherPage.aspx?uid=" + txtName.Text + "&pwd=" + txtPwd.Text")           Case Else                     Response.Redirect("StudentPage.aspx")End Select

Request object

The main function is to obtain data from the client, including form-based data and parameter list information sent through the URL, and receive cookie information from the user.

Difference between post and get

In Asp.net, data is sent to the server through a form. There are two data transmission methods (HTML): Post and get. Get attaches the transmitted data to the end of the URL. The post method encapsulates the transmitted data as a dataset for transmission.

Reading information during post

String strusername = request. Form. Get ("txtusername"). tostring (); note that form is equivalent to a set. Therefore, there are multiple methods to obtain the corresponding data. String strusername = request. Form ["txtusername"]. tostring ();

Get method information reading

String strusername = request. querystring ["txtusername"]. tostring (); string strusername = request. querystring. get ("txtusername "). tostring (); request ["txtusername"]. tostring ();

Summary

Either way, you can use this method: request ["txtusername"]. tostring (); to obtain data.


Server Object

Objects that provide server functions (interactions.

Attribute

Machinename: get the name of the computer where the server is located

Scripttimeout: Get and Set Request timeout (in seconds)

Method

Urlencode, urldecode, htmlencode, and htmldecode: As mentioned in the previous blog, the unified encoding method for data transmission is to convert the special data into normal transmission, reverse Engineer those common characters to obtain the real data and prevent garbled characters. From this we can launch, now the data encoding method of HTTP transmission is still ASCII code, so that the special characters (Chinese characters) that do not have the corresponding code will appear, and will be garbled, in this way, distortion occurs only when it is displayed.

Mappath: converts a virtual path or relative path relative to the current page to a physical file path on the Web server. Server. mappath ("/default. aspx ");


Application Object

The application object is equivalent to a set in which a group of variables can be recorded. These variables are composed of key values, data can be operated by all client users (each socket.

The basic operations are as follows:

Add or create an object

Application. Add ("key", value); or application ("key") = value;

Obtains the value of an object in the application set.

Int I = (INT) Application ("key"); or Int I = application. Contents ["key"]; or Int I = application. Get ("key ")

Because all objects in the set are objects, type conversion is required.

Update object values in a set

Application. set ("key", value); or application ["key"] = (INT) application ["key"] + 1; it is best to add the lock mechanism when updating: application. lock () and application. unlock ().

Remove objects

Application. Remove ("key"); remove the specified object. Application. removeall (); or application. Clear () removes all objects.


Basic events

Application_start, application_end, and application_error. These events are in the global. asax file. Global. the asax file is an optional (No, only one) file that contains the application-level and session code (events) caused by the response to the Asp.net application or HTTP module)


Session Object

Saves information related to the current user session, similar to the application. The current user is a socket within a period of time. This object makes the variables stored in the session object unclear when the current user jumps between pages of the application.

The session operation is the same as the above. You only need to replace the application with the session operation. The global. asax file also has corresponding start and end events.


Cookie object

Collection objects, similar to application and session objects, can be used to save data information, but cookies store data on the client's hard disk, the application and session objects store data on the server.

Cookies are divided into session cookies and persistent cookies. The session cookie does not exist after the session ends, and the persistent cookie does not exist at the specified time.

Create a cookie with a single value

 Response.Cookies["UserID"].Value = "1"; Response.Cookies["UserID"].Expires = DateTime.Now.AddMonths(1);

The two codes mean to deliver a single-value cookie to the browser. Valid time: one month from now on. After this code is executed, the browser will receive the cookie. At this time, the browser will automatically store the cookie information to a location on the hard disk without writing any code on the client, when you access any webpage on this website again, the browser automatically sends the corresponding cookie.

Receive single-value cookies

string strCookie = Request.Cookies["UserID"].Value; 

Create multi-value cookie

            Response.Cookies["V"]["UserID"] = "1";            Response.Cookies["V"]["UserPWD"] = "2";            Response.Cookies["V"].Expires = DateTime.Now.AddMonths(1);

Receive multi-value cookies

Httpcookie objcookie; objcookie = request. cookies ["v"]; If (objcookie = NULL) {response. write ("cookie is empty");} else {string strname = objcookie. values ["userid"]; string strpwd = objcookie. values ["userpwd"]; response. write (strname. tostring (); response. write (strpwd. tostring ());}

Configuration File

Let's just talk about the configuration of Web. config. For details and explanations, see the code and corresponding comments.

<! -- Root node of the configuration file --> <configuration> <! -- Configure the entire application --> <system. Web> <! -- Set cache for all pages of the application; enable the viewstate function --> <pages buffer = "true" enableviewstate = "true"/> <! -- When an error occurs during browser access, the server will return to the browser through a friendly interface --> <customerrors defaultredirect = "URL of the user-friendly error interface" mode = "on | off | remoteonly"> <! -- When the HTTP status code of the error page is 500, the browser displays the URL; otherwise, the default --> <error statuscode = "500" Redirect = "url"/> </customerrors> </system. web> <! -- This is the format of vs 03: User-defined settings --> <appsettings> <add key = "strconn" value = "Server = .; uid = sa; Pwd = password; database = pubs "> </Add> </appsettings> <! -- The same as the appsettings function: User-defined settings; format of vs 05 and later --> <connectionstrings> <Add name = "strconn" connectionstring = "server .; uid = sa; Pwd = password; database = pubs "> </Add> </connectionstrings> </configuration>

Obtain the value in the Custom region from the configuration file.

First, add reference: system. configuration.

The code for accessing the variables in the <deleetask> node is as follows:

//string a = ConfigurationManager.AppSettings["sqlConnectionString"].ToString();string a = ConfigurationManager.AppSettings["sqlConnectionString"].ToString();

The code for accessing variables in the <connectionstrings> node is as follows:

//string myCon = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;string myCon = ConfigurationManager .ConnectionStrings ["strCon"].ToString ();

For more information about the configuration file, refer to this blog: VB.net configuration file.


Learning Process

First, I watched the video "Sky wear". I just watched it. The examples in the video were not typed, And then I watched "Peking University qingbird" (most of the examples in it were implemented). Then, that is to sum up, it takes a long time in total, from April 1st to April 10th, almost a month. After excluding the meeting time and the time for learning English, the time for learning Asp.net is also quite a lot, oh, my God. In the future learning process, videos will not be the focus, but the amount to absorb will be counted.

Summary

There are still many things to learn. Continue to work!

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.