Asp. NET Event Small Discussion

Source: Internet
Author: User
asp.net did the ASP.net page counter experiment this morning with a simple text file to store the counter's value (
is actually stored in byte type value), the principle code can be seen in my previous "Simple counter implementation program."

Today's task is to put the original WinForm file read and write into the ASP.net behindcode, which will appear
A bit of a problem.

Problem One: File path
Writing a file path in ASP.net's behindcode cannot be written directly by the file in the directory of this application
The name of the piece to invoke.
Cases:
Before using WinForm programming, if the text file Count.txt with the program executable file in a directory
FileStream fs=file.open ("Count.txt", FileMode.Open) can be used directly to read the file
Take.
In asp.net, the path to the Web application is "virtual path," meaning it is not mapped to the system's current application
Default path (the system's current default path should be C:\windows\system32 because there is a Web application under this path)
The program is available for invocation by the rich Win32API). So where is the root of a virtual path stored? should be in I
In the configuration file of is, this path is mapped through the Server.MapPath () function in C #.
Cases:
If the Count.txt file is in the directory of this Web application, you can pass the Server.MapPath (".. /count.tx
T "), to map to this file. Note: The file cannot be found directly by writing the filename, which shows that the file cannot be read
Information.

Question two:
Web Application Event Issues
In fact, there are three levels of asp.net communication and access models
The first level is application, the Web application layer
This level of variable cycle for the entire application, but in Application_Start and Application_End two
event, which can only be performed at the beginning or end of the application (server Startup/shutdown or
The user forces IIS to start/Shut down the application, so it is useless to write the counter code in both events
Of
The second level is the session, which is a communication layer of the Web application
One communication with the user (starting with an IP Access application, to this IP to disconnect from the application or timeout
On the counter is the most valuable embodiment, but also a page counter function.
But the problem has arisen:
The Session_Start and Session_End events of the session are fired at the beginning and end of a communication. This
If you write code in Session_Start real, you only have a count on a page that is accessed at the start of a communication
The display of the device, and after refreshing the page display counter parts will not be shown again, how to do? No hurry, please.
Continue to look down.
The third level is the page layer, which is the control object layer for each page, Web document.
We know that a Web page has a page layer and we will understand that when a page is paged out, it will call the PA
Add the counter count code in this event, and the counter will increase by one when the page is refreshed, which is our
Do not want to see.
What to do?
Very simple, write counter count code in Session_Start, write counter to display code in Page_Load
, so that we can achieve the functionality we need.

Hopefully, this article will help beginners understand these Web application levels and their corresponding events, sometimes
Can have the wrong opinion of the problem, hope to have master advice.


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.