asp.net development of some common skills _ practical skills

Source: Internet
Author: User
Tags httpcontext
Do not use page caching:
You can add the following code to the page Page_Load event that you do not want to be cached
Copy Code code as follows:

Response.Expires = 0;
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds (-1);
Response.AddHeader ("Pragma", "no-cache");
Response.CacheControl = "No-cache";

To compile into a DLL:

"Start"-"Run"-"cmd" and compile the AuthCode.cs file as a. dll file with the following two commands. (The AuthCode.cs file is saved in the "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727" directory) command as follows:

CD C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
Csc/target:library AuthCode.cs
GridView exported to Excel code 2:
Copy Code code as follows:

public override void Verifyrenderinginserverform (Control control)
{
(Must have)
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 Code code as follows:

Scriptmanager.registerstartupscript (this. Page, GetType (),
"Askandrederect", "alert (' please log in first! '); window.location.href= ' index.aspx '; ", true);

query before deleting a row in the GridView:
Copy Code code as follows:

((LinkButton) e.row.cells[4]. CONTROLS[2]). Attributes.Add ("onclick", "Javascript:return confirm") (Are you sure you want to delete it?) ')");

GridView Interlace color code:
Copy Code code 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 background color When the mouse moves away
E.row.attributes.add ("onmouseout", "this.style.backgroundcolor=c");
}
}

asp.net regular expression:
Copy Code code as follows:

Verify the phone number
Regex r = new Regex (@ "^ (13|15|18) \d{9}$");
Return R.ismatch (Mobilephone);
With a cell phone number 86 or +86.
Regex regexmobile = new Regex (@ "^" (\+86) | ( 86))? (13|15|18) \d{9}$ ");
Verify if it is Chinese
Regex regex = new Regex ("^[-龥]+$");
Validation of decimal for decimal (8,4) type
Regex rx = new Regex (@ ^-?: [1-9][0-9]{0,3}|0) (?: \. [0-9] {1,4})? $");
Verify that the input is a combination of numbers and letters
regex = new Regex ("^[-龥 _a-za-z0-9]+$");
Verify that the input is all numeric
regex = new Regex ("^[0-9]+$");
Verify that the input is all Chinese
regex = new Regex ("^[-龥]+$");
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 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 that associates the button with the ENTER key:

The solution to setting the ENTER key in a Web page is to use the JavaScript Document.onkeydown () method to capture the keyboard click events and use Event.keycode to get the keys that the user clicks on.
Copy Code code as follows:

function Document.onkeydown ()
{
if (Event.keycode = 13)
{
Button.Click ()//Click on the ENTER key to invoke button's Click event
Event.returnvalue = false;//Cancel the default action of the ENTER key
}
}

If the button button is a server-side pushbutton, change the following
Copy Code code as follows:

function Document.onkeydown ()
{
Using document.getElementById to get to a button object
var button = document.getElementById (' <=serverButton.ClientID%> ');
if (Event.keycode = 13)
{
Button.Click ();
Event.returnvalue = false;
}
}

If the button is in a user control, the above method can be used in a user control.
Be sure to cancel the default action for the ENTER key, or the default button will continue after you execute the button button.
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.