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 server-side alert dialog box is displayed /// </Summary> /// <Param name = "str_message"> the message is displayed. 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) ///// the pop-up dialog box ///// pointer to the current page, this // message is generally 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 the random number // The 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