In ASP.NET use javascript point tips

  We in the ASP.NET development, often used some javascript script, such as: 

  Private void Button1_Click (object sender, System.EventArgs e) 
  ( 
  Response.Write ( "<script language='javascript'> alert ( 'OK');</ script>"); 
  ) 

  Often repetitive writing these scripts, and if we can cause a corresponding function like, and can be used directly on the use.    Many people have some of their own javascript function, but most to this: 

  / / / <summary> 
  / / / Server pop-up dialog box alert 
  / / / </ Summary> 
  / / / <param Name="str_Message"> messages, for example: "Please enter your name!" </ Param> 
  / / / <param Name="page"> Page category </ param> 
  Public void Alert (string str_Message, Page page) 
  ( 
  If (! Page.IsStartupScriptRegistered ( "msgOnlyAlert")) 
  ( 
  Page.RegisterStartupScript ( "msgOnlyAlert", "<script> alert ( '" + str_Message +"');</ script> "); 
  ) 
  ) 

  However, when used, should inherit this class each, or together with some trouble, if the function is static function, is the kind of static, we do not inherit can use.    But how we write? 

  Look at this code 

  # Region public static void MessageBox (Page page, string msg) 
  / / / 
  / / / Pop-up dialog box 
  / / / 
  / / / Current page of the guide, for this general 
  / / / News 
  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 

  If this is the case we will be able to facilitate the use of many existing js script. 

  PS: In fact, many commonly used method can be written static function calls.    Even more of a few examples as a reference. 

  MD5 encryption: 

  / / / 
  / / / MD5 Encrypt 
  / / / 
  / / / Text 
  / / / Md5 Encrypt string 
  Public string MD5Encrypt (string strText) 
  ( 
  MD5 md5 = new MD5CryptoServiceProvider (); 
  Byte [] = md5.ComputeHash result (System.Text.Encoding.Default.GetBytes (strText)); 
  Return System.Text.Encoding.Default.GetString (result); 
  ) 

  From the designated length of the random number: 

  # Region public static string GetRandNum (int randNumLength) 

  / / / 
  / / / 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 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: , ,

Releated Articles


0 Comments to “In ASP.NET use javascript point tips”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.