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)
{
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?
Take a look at this sectionCode
# Region Public static void MessageBox (page, string MSG)
///
/// Dialog box
///
/// Pointer to the current page, usually this
/// Message
Public static void MessageBox (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