Summary of global. asax experience

Source: Internet
Author: User

1. Summary of global. asax experience

Description: global. asax is the global application variable in asp.net. For other information, go to msdn!
 
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////
 
<% @ Application Language = "C #" %>
 
<Script runat = "server">
 
Public static int run_time;
 
Public int run_time2;
 
Protected void Application_BeginRequest (Object sender, EventArgs e)
 
{
Application ["StartTime"] = System. DateTime. Now;
}
 
Protected void Application_EndRequest (Object sender, EventArgs e)

{
System. DateTime startTime = (System. DateTime) Application ["StartTime"];

System. DateTime endTime = System. DateTime. Now;

System. TimeSpan ts = endTime-startTime;
Run_time = ts. Milliseconds;
}
Void Application_Start (object sender, EventArgs e)

{

// Code that runs when the application starts

}
Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed
}
Void Application_Error (object sender, EventArgs e)
 
{

// Code that runs when an unhandled error occurs
 
}
 
Void Session_Start (object sender, EventArgs e)

{
// The code that runs when the new session starts
}

Void Session_End (object sender, EventArgs e)

{

// The code that runs when the session ends.

// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session mode is set to StateServer

// Or SQLServer, the event is not triggered.

}
</Script>
 
Because I am doing a website search function, I need to get the page execution time like baidu, and I checked the information online, it is best to get it on the server, to use the asax file, and then put the code to global according to the online instructions. asax. cs, but found that my project does not have this file, so Add a new project, found only one add global. asax global application. I put it in the global folder and came out. cs file, but after the code is copied, no effect is found during the operation. After one afternoon, the mysterious guy is still not found in multiple searches )! Communicate with friends, saying that you can get a new project, but I didn't see any trace of it, so I thought my vs was broken. Later, the "Maple Leaf" (Network Name) friend suggested: copy it directly. The result is displayed. Haha!
I will briefly describe how to use this guy and pay attention to depressing things! Code ):

The code for global. asax is above
 
In serach. aspx
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;

Using System. Web;
Using System. Web. Security;
Using System. Web. UI;

Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
 
Public partial class serach: System. Web. UI. Page
 
{
 
ASP. global_asax B = new ASP. global_asax (); // defines the B object, a bit like a class, and some say it is an interface.
 

Protected void Page_Load (object sender, EventArgs e)
Response. Write ("Page execution time" + ASP. global_asax.run_time + "millisecond"); // call class variables
Response. Write ("Page execution time" + B. run_time + "millisecond"); // call the member variable

}
 
}
 
 
Conclusion: In global. writing code or methods in asax is just like writing a class, but the call is very unstable. Sometimes vs is not automatically clicked, and there is no prompt, as if there is no such variable! The above is the call variable, and the call method is the same. Of course, the second member variable must be 0, so I use the first static variable.
 
 
In addition, it is found that only one asax file can be added in vs. It is useless to add more files!
 
 
We don't need to worry about the mysterious guy global. asax. cs! Write it directly in asax.
 
If I'm not correct, forget to correct it!
2. Notes on the custom control ascx
I used user-defined controls in the project and found that in ascx, js is not very well supported. If we add js effects to images in ascx, in addition, we wrote a funcition function and found the reference page, such as index. aspx cannot find this object at all, indicating that this js definition is not supported. After my experiment, I will put js on the reference target page, in fact, the principle of application is the old asp reference of dianxiang <indel>. However, ms's custom control is more suitable for functional row controls, such as the voting system, we can make a control to make good use of it!

Related Article

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.