Some common skills for Asp.net Development

Source: Internet
Author: User

Do not use page cache:
You can add the following in the page_load event of the page that you do not want to cache: Code Copy code The Code is as follows: Response. expires = 0;
Response. Buffer = true;
Response. expiresabsolute = datetime. Now. addseconds (-1 );
Response. addheader ("Pragma", "No-Cache ");
Response. cachecontrol = "no-Cache ";

Compile to DLL:

"Start"-"run"-"cmd", and use the following two commands to compile the authcode. CS file as the. dll file. (The authcode. CS file is saved in the "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727" directory.) The command is as follows:

Cd c: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727
CSC/Target: Library authcode. CS
Code 2: Copy code The Code is as follows: public override void verifyrenderinginserverform (Control)
{
// (Required)
// Base. verifyrenderinginserverform (control );
}
Public void generate (string typename, string scswid, gridview tempgrid)
{
Httpcontext. Current. response. Clear ();
Httpcontext. Current. response. charset = "UTF-8 ";
String filename = typename + scswid + ". xls ";
Httpcontext. Current. response. appendheader ("content-disposition", "online; filename =" + filename );
Httpcontext. Current. response. contentencoding = system. Text. encoding. getencoding ("UTF-8 ");
Httpcontext. Current. response. contenttype = "application/MS-excel ";
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
Tempgrid. rendercontrol (ohtmltextwriter );
Httpcontext. Current. response. Write (ostringwriter. tostring ());
Httpcontext. Current. response. End ();
}

Prompt and jump:
Copy codeThe Code is as follows: scriptmanager. registerstartupscript (this. Page, GetType (),
"Askandrederect", "alert ('Log On first! '); Window. Location. href = 'index. aspx'; ", true );

Query before deleting a row in the gridview: Copy codeThe Code is as follows: (linkbutton) E. row. cells [4]. controls [2]). attributes. add ("onclick", "javascript: Return confirm ('Are you sure you want to delete it? ')");

Code for changing the color of the gridview line:Copy codeThe Code is as follows: protected void gvlinkman_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
E. Row. Attributes. Add ("onmouseover", "c = This. style. backgroundcolor; this. style. backgroundcolor = '# 00a9ff '");
// Restore the background color when you move the mouse away.
E. Row. Attributes. Add ("onmouseout", "This. style. backgroundcolor = C ");
}
}

Asp.net regular expression: Copy codeThe Code is as follows: // verify the mobile phone number
RegEx r = new RegEx (@ "^ (13 | 15 | 18) \ D {9} $ ");
Return R. ismatch (mobilephone );
// Mobile phone number with 86 or + 86
RegEx regexmobile = new RegEx (@ "^ (\ + 86) | (86 ))? (13 | 15 | 18) \ D {9} $ ");
// Verify whether it is Chinese
RegEx = new RegEx ("^ [1-Snapshot] + $ ");
// Decimal (8, 4) type decimal Verification
RegEx RX = new RegEx (@ "^ -? (? : [1-9] [0-9] {0, 3} | 0 )(? : \. [0-9] {1, 4 })? $ ");
// Verify whether the input is a combination of numbers and letters
RegEx = new RegEx ("^ [A-prop _ a-zA-Z0-9] + $ ");
// Verify whether all input values are numbers
RegEx = new RegEx ("^ [0-9] + $ ");
// Verify that all inputs are in Chinese
RegEx = new RegEx ("^ [1-Snapshot] + $ ");
// Email
RegEx = new RegEx (@ "^ ([\ W-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ W-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ");
// Verify the URL
RegEx = new RegEx (@ "HTTP (s )? : // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)? ");
// Phone number
RegEx = new RegEx (@ "(\ D {3 })? \ D {8} | (\ D {4}) (\ D {7 })");

The js method for associating a button with the Enter key:

The solution for setting the Enter key on the webpage is to use the document. onkeydown () method of JavaScript to capture keyboard click events, and use event. keycode to obtain the key position clicked by the user.Copy codeThe Code is as follows: function document. onkeydown ()
{
If (event. keycode = 13)
{
Button. Click (); // click the Enter key to call the button click event
Event. returnvalue = false; // cancel the default operation of the enter key
}
}

If the button is a server-side button, the changes are as follows:Copy codeThe Code is as follows: function document. onkeydown ()
{
// Use document. getelementbyid to obtain the Button Object
VaR button = Document. getelementbyid ('<= serverbutton. clientid %> ');
If (event. keycode = 13)
{
Button. Click ();
Event. returnvalue = false;
}
}

If the button is in the user control, the above method can be used in the user control.
You must cancel the default operation of the enter key. Otherwise, the default button will continue after the button is executed.

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.