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 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) ///// the pop-up dialog box ///// pointer to the current Page, this // message is generally 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 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
From: http://blog.sina.com.cn/s/blog_4efce4d10100cksm.html