Recently, when a company launched a project, it often crashed, so it began to pay attention to performance issues. On the Internet, I saw many related articles and sorted them out. For reference.
When developing a website using ASP. NET, performance always needs to be considered and concerned. performance is not only the speed at which the program code is executed, but also related to all aspects.
Take ASP. for a request from the web browser to the server's ASP.. NET website sends the request until the end of the page is displayed in front of us. each step of the request has a different tuning method, and there are many calling methods, it is not just common: caching, multithreading, and Asynchronization.
The articles in this series decide to explain Optimization in two major aspects:
Front-end optimization: It mainly includes how to minimize http requests, from http requests to loading js and css, and how to compress transmitted data.
Background optimization: Analysis of ASP.. NET Request Processing Process, and the corresponding optimization method is provided in each step, and the optimization method is provided in the Code Organization, architecture and database operations.
I remember that when I was developing a website, when I mentioned how to improve performance, the easiest and fastest possible thing to think of was caching. In some Microsoft Best Practice documents, I also suggested: layer-by-layer caching (caching at the data storage layer, DAL, BLL, and UI ). Then, on the website, "the cache is blooming everywhere", and the final result is indeed unsatisfactory.
Another common optimization is for databases: for example, to minimize the number of subqueries, join is used; and indexes are created on fields that are frequently queried. Indeed, these are common and good rules.
Another experience is that, when optimizing performance, if you choose to optimize code and database, some database operations are often optimized to bring better results, but unfortunately: in projects (at least in some projects I have developed), databases are just a data storage device, and they have not played a powerful role in the database. Therefore, it is recommended that you be familiar with the internal query and storage mechanisms of databases. After all, many developers also work as DBAs (many companies do not have formal DBAs ).
In addition, when designing databases in projects, especially table fields, we need to consider some issues. Many people suggest that the length of table fields should not be too long. This is also a common recommendation, but why? In fact, we need to understand some internal storage mechanisms of the database: When the database (SQL SERVER) is saved, the data is in the smallest unit of "page, each page has a size of 8 K. If the data in your table exceeds 8 K, the data in this table will be saved on several pages. In this way, when querying the data, cross-page query is required. Cross-page query requires performance consumption. If data is on one page, the speed is certainly faster.
Therefore, to optimize the Website, you must know the performance consumption.
When optimizing a website, it is not blindly generalized. Generally, there are two situations:
1. The website already exists and runs. Now we need to optimize it.
2. A new website is being developed from scratch.
If this is the first case, we should first find out the bottleneck of the website performance, from the front-end request to the back-end request processing, until the presentation of the last page, all of which should be reviewed step by step.
In the second case, the situation may be slightly better, and the website is completely controlled by us now. Many optimization principles can be used in all development and design processes.
Optimization does not necessarily mean code rewriting or making major changes. A little bit of accumulation during optimization is like code refactoring, which is a cumulative effect. For example, whether to load the js script at the beginning of the page or load the js script at the end of the page, sometimes it is just a simple adjustment of the loaded file, or asynchronously load the script, or transmit the script through CDN, the performance is improved. The performance improvement is not zero, and some costs are very small. For example, if you just load the script to the end of the page, the big cost is, for example, buy some server devices, for example, Content Delivery Network (CDN) is used to transmit static files (js, css, and image) to the client. Therefore, optimization requires a balance strategy.
The following are some common problems and solutions for interaction between javascript and c #.
1. How to access the C # function in javaScript?
2. How to access the C # variable in Javascript?
3. How to access existing JavaScript variables in C?
4. How to access JavaScript Functions in C?
The answer to question 1 is as follows:
Execute functions in C # code in javaScript Functions:
Method 1: 1. Create a button to write the called or processed content to button_click in the background;
2. Write a js function at the front end. The content is
[Javascript]
Document. getElementById ("btn1"). click ();
3. Call js functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the c # function in the background;
Method 2: 1. function declaration is public
Background code (change public to PRotected)
[Csharp]
Public string ss ()
{
Return ("");
}
2. can be called in html
Foreground script
[Javascript]
<Script language = javascript>
Var a = "<% = ss () %> ";
Alert ();
</Script>
Method 3: 1,
[Javascript]
<Script language = "javascript">
<! --
Function _ doPostBack (eventTarget, eventArgument)
{
Var theForm = document. Form1; // indicates the form of runat = server.
TheForm. _ EVENTTARGET. value = eventTarget;
TheFrom. _ EVENTARGUMENT. value = eventArgument;
TheForm. submit ();
}
-->
</Script>
<Input id = "Button1" type = "button" name = "Button1" value = "button" onclick = "javascript :__ doPostBack ('button1','') ">
Method 4:
[Javascript]
<Script language = "javascript">
Function SubmitKeyClick ()
{
If (event. keyCode = 13)
{
Event. cancelBubble = true;
Event. returnValue = false;
Document. all. FunName. value = "Name of the function you want to call ";
Document. form [0]. submit ();
}
}
</Script>
<INPUT onkeypress = "SubmitKeyClick ()" id = "aaa" type = "text">
<Input type = "hidden" name = "FunName"> <〈! -- Used to store the function you want to call --> --〉
In. CS, there are:
[Csharp]
Public Page_OnLoad ()
{
If (! Page. IsPost ())
{
String strFunName = Request. Form ["FunName"]! = Null? Request. Form ["FunName"]: "";
// Determine which function to call based on the value returned
Switch (strFunName)
{
Case "enter ()":
Enter (); // call this function
Break;
Case "Others ":
// Call other functions
Break;
Default:
// Call the default Function
Break;
}
}
}
Public void enter ()
{
//...... For example, calculating a value
}
Question 2. How to access the C # variable in JavaScript?
The answer is as follows:
Method 1: 1. Hide domain access on the page
[Html]
<Input id = "xx" type = "hidden" runat = "server">
Method 2: 1. If the backend defines public string n, the format of the variable referenced in front-end js is ''or" ++"
Method 3: 1. You can register a script on the page after assigning values to variables on the server.
"<Script language = 'javascript '> var temp =" + tmp + "</script>"
Tmp is the background variable, and then you can directly access temp in js to obtain the value.
Question 3. How to access existing JavaScript variables in C?
The answer is as follows:
Method 1: 1. the foreground uses static text controls to hide the field and write the js variable value into it. 2. The background uses request ["id"] to obtain the value;
Method 2: cookie or session can be used
Question 4. How to access JavaScript Functions in C?
The answer is as follows:
C # execute javaScript Functions in the Code:
Method 1: 1,
[Csharp]
Page. RegisterStartupScript ("ggg ","");
Method 2: Use the Literal class and
[Csharp] www.2cto.com
Private void Button2_Click (object sender, System. EventArgs e)
{
String str;
Str = "";
// Literal1.Visible = true;
Literal1.Text = str;
}