Asp.net practical small Functions

Source: Internet
Author: User
Some javascript scripts are often used during ASP. NET development, such:
Private void button#click (object sender, System. EventArgs e)
{
Response. Write ("<script language = 'javascript '> alert (' OK '); </script> ");
}

These scripts are often written repeatedly. If we can make a corresponding function, we can use it directly. Many people have their own javascript Functions, but most of them look like this:

/// <Summary>
/// The alert dialog box is displayed on the server.
/// </Summary>
/// <Param name = "str_Message"> message. For example, enter your name! "</Param>
/// <Param name = "page"> Page class </param>
Public void Alert (string str_Message, Page page)
{
If (! Page. IsStartupScriptRegistered ("msgOnlyAlert "))
{
Page. RegisterStartupScript ("msgOnlyAlert", "<script> alert ('" + str_Message + "'); </script> ");
}
}

However, it is still troublesome to use this class every time you use it. If the function is a static function and the class is a static class, we can use it without inheritance. But how do we write it?

Look at this code

# Region public static void MessageBox (Page page, string msg)
///
/// Dialog box
///
/// Pointer to the current page, usually this
/// Message
Public static void MessageBox (Page page, string msg)
{
StringBuilder StrScript = new StringBuilder ();
StrScript. Append ("<script language = javascript> ");
StrScript. Append ("alert ('" + msg + "');");
StrScript. Append ("</script> ");
If (! Page. IsStartupScriptRegistered ("MessageBox "))
{
Page. RegisterStartupScript ("MessageBox", StrScript. ToString ());
}
}
# Endregion

In this way, we can easily use many existing js scripts.

PS: in fact, many common methods can be written as static functions for calling. A few examples are provided for reference.

MD5 encryption:

///
/// MD5 Encrypt
///
/// Text
/// MD5 encrypt string
Public String md5encrypt (string strtext)
{
MD5 MD5 = new md5cryptoserviceprovider ();
Byte [] result = md5.computehash (system. Text. encoding. Default. getbytes (strtext ));
Return System. Text. encoding. Default. getstring (result );
}

Random number with the specified length:

# Region Public static string getrandnum (INT randnumlength)

///
/// Obtain a random number
///
/// Length of the random number
///
Public static string getrandnum (INT randnumlength)
{
System. Random randnum = new system. Random (unchecked (INT) datetime. Now. ticks ));
Stringbuilder sb = new stringbuilder (randnumlength );
For (int I = 0; I <randNumLength; I ++)
{
Sb. Append (randNum. Next (0, 9 ));
}
Return sb. ToString ();
}

# Endregion

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.