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