After several years of ASP. NET, I have accumulated a lot of tips. However, skills are only suitable for specific environments. It is better to strengthen the understanding and application of basic knowledge. The following is my experience.
1 master page
This control can make me forget the page framework scheme such as html iframe and frameset.
ASP development also supports <# include file = "head. asp>
Currently, you can easily write pages in. Net mode by using a master page.
1) on the Content Page corresponding to the master page, find the control on the master page and assign it a value.
Masterpage. findcontrol ("lbltitle") = "new request"
2) use JavaScript on the master page to obtain the value of the text control and verify that it is legal.
'<= Txtusername. clientid>'
2 user control is not commonly used, but is sometimes used. For example, to unify the page addresses of different users, place pages of different roles in user control. On the same page, load different user control items based on the current Login User's judgment.
Create two files. PM. ascx and GM. ascx correspond to the pages where the roles are PM and GM respectively.
Usercontrol UC
Swith (role)
{
Case "PM"
UC = (usercontrol) loadcontrol ("~ /PM. ascx "); break;
Case 'GM"
UC = (usercontrol) loadcontrol ("~ /GM. ascx "); break;
}
Placeholder. Controls. Add (UC );
Placeholder is a placeholder control placed on the page.
3 data source control usage example
BIND database fields to the dropdownlist control.
datavaluefield = "shortname" datasourceid = "sqlendcustomer">
selectcommand =" select ltrim (rtrim ([shortname]) [shortname] from [Customer] ">
next xmldatasource
the XML file format is as follows
you are not familiar with objectdatasource and are not used in project development. Sqldatasource is rarely directly used for data binding in the gridview. data in the database usually needs to be converted. You need to re-write the Code using this control, it is better to write code directly.
for example, there is a field in the database, smallint type, 1 indicates male, 0 indicates female. Use the Program to check whether the program can be bound to the control. The SQL statement is as follows:
select sex = case when sex = 1 then 'man '
when sex = 0 then 'Woman'
end
from employee
4. Resource files
Generally, some prompt strings, such as "operation successful" and "new business order failed", are scattered in various classes, which is difficult to manage and difficult to unify the style. Resource source files can effectively solve this problem.
Create 2 files, strings. aspx. en-us.resx, strings. aspx. resx
Enter the key for the value of the string resource in the file and call it in the background code of the page.
Bitmap IMG = (system. Drawing. Bitmap) getglobalresourceobject (
"Myglobal", cultureinfo. currentculture. Name );
(String) getglobalresourceobject ("myglobal", "globaltext ");
(String) getlocalresourceobject ("LBL. Text ");
As shown in the code, resources can be retrieved from global resource files or partial resource files, which can be strings or images.
5. Verify the control
<Asp: textbox id = "txtusername" runat = "server"/>
<Asp: requiredfieldvalidator id = "required" runat = "server" errormessage = "requiredfieldvalidator" display = "NONE" controltovalidate = "txtusername"> </ASP: requiredfieldvalidator>
The authentication username must be input; otherwise, submission is not allowed.
If you use the verification control, the default attribute causesvalidation = true of the button on the page triggers verification.
I helped my colleagues see a problem last week, such
This is the user selection page. Click> to add the employee to The ListBox on the right.
My colleague copied my code and clicked the> button. The page events did not respond, and the debugger was not interrupted.
I checked his page code carefully and found that he used ASP.. Net verification control. The verification control itself does not display an error message, as shown in the Code: Display = "NONE". Put all verification prompts in validationsummary, however, the control is not copied in his code, so the problem cannot be found. I told him to set the> button's causesvalidation to false and the button starts to work normally.
6 themes is not used much. It is better to set themes in CSS files.
Although membership is scalable, Ms has made so many stored procedures, the relationship is complex, and it is rarely used by itself.
I have seen that in the win forms application, the application aspnetdb is used as the user verification database and is not used in Web development. Someone has applied it in win forms. It can be seen that it still has a typical meaning. Aspnetdb also provides a management tool that can directly add users and add roles. This allows you to quickly build a role permission database and verify system users.
Security is usually form verification. Our company's internal LAN manages and trusts each other in the form of a domain. Instead, we use the Windows authentication method and use the login ID of each computer as the primary user name. When we find that this user does not exist in the database, call up the pilot registration page and ask the user to register. Then the administrator can review his role and grant the corresponding permissions.
Profile is rarely used. It is estimated that this is closely related to the portal framework, and it is almost useless.
You have mentioned a requirement to configure some default values for each input form to facilitate quick data input. Because the role is almost unchanged, the data to be input is usually in those categories. It is not convenient for me to make a dropdownlist for him to choose. It is best to help him select a default value directly, the default value is 80%.
It is estimated that this function will be easier to use profile, and this feature will be used in the new version.
6 cache
in ASP. NET 1.1 has worked as a solution to implement a multi-language ASP. net Applications, place the language in XML, write the zh-cn.xml, en-us.xml, zh-tw.xml, then read the file in the interface, assign a value to the interface element.
when cache is not used, some people suggest that the content of this file will hardly change. You should take the initiative to use the file dependency cache to speed up reading.
another competitor is whether to use httpcontext. cahce or httpruntime. cache. The page cache must be the worst solution. At that time, I was stupid and did not know the relationship between the two. Now you should know that you use one, or you need to check an msdn.
the previous Code section is available.
If (Cache ["key"] = NULL)
{< br> cache. insert ("key", datetime. now, null, datetime. now. addseconds (10), timespan. zero);
}< br> datetime dt = (datetime) cache ["key"];
the Cache Usage mode is to determine whether the cache exists first, the cache does not exist. Otherwise, the value
If (Cache ["name"] = NULL)
cache. insert ("name", datetime. now, null, datetime. now. addseconds (10), timespan. zero);
txtusername. TEXT = convert. tostring (Cache ["name"]);
when there are a large number of cache key-value pairs, it is best to make a cachemanager to manage the key-value pairs in a unified manner to reduce errors.