ASP. NET c # encapsulate common js Functions

Source: Internet
Author: User

Using System. Web;
Using System. Web. UI;
Using System. Text;

Public abstract class JS
{
/** // <Summary>
/// Open the client window
/// </Summary>
/// <Param name = "strUrl"> </param>
Public static void OpenWindow (string strUrl)
{
HttpContext. Current. Response. Write ("<script language = javascript> ");
HttpContext. Current. Response. Write ("window. open ('" + strUrl + "');");
HttpContext. Current. Response. Write ("</script> ");

}
/** // <Summary>
/// Output Custom Script Information
/// </Summary>
/// <Param name = "page"> current page pointer, which is generally this </param>
/// <Param name = "script"> output script </param>
Public static void ResponseScript (System. Web. UI. Page page, string script)
{
Page. RegisterStartupScript ("message", "<script language = 'javascript 'defer>" + script + "</script> ");
}
/** // <Summary>
/// Display the message prompt dialog box and jump to the page
/// </Summary>
/// <Param name = "page"> current page pointer, which is generally this </param>
/// <Param name = "msg"> message </param>
/// <Param name = "url"> Target URL </param>
Public static void ShowAndRedirect (System. Web. UI. Page page, string msg, string url)
{
StringBuilder Builder = new StringBuilder ();
Builder. Append ("<script language = 'javascript 'defer> ");
Builder. AppendFormat ("alert ('{0}');", msg );
Builder. AppendFormat ("top. location. href = '{0}'", url );
Builder. Append ("</script> ");
Page. RegisterStartupScript ("message", Builder. ToString ());
}
 
 
Public static void ShowConfirm (System. Web. UI. WebControls. WebControl Control, string msg)
{
Control. Attributes. Add ("onclick", "return confirm ('" + msg + "');");
}
/** // <Summary>
/// Clear textbox content in all panels
/// </Summary>
/// <Param name = "pan"> </param>
Public static void clearAll (System. Web. UI. WebControls. Panel pan)
{
Foreach (Control panl in pan. Controls)
{
If (panl. GetType (). ToString () = "System. Web. UI. WebControls. TextBox ")
{
(System. Web. UI. WebControls. TextBox) panl). Text = "";
}
}
}

/** // <Summary>
/// Dialog box displayed after the form is loaded
/// </Summary>
Public static void Alert (string msg)
{
Page pages;
Pages = HttpContext. Current. Handler as System. Web. UI. Page;
Msg = msg. Replace ("'","");
Msg = msg. Replace ("\"","");
Msg = msg. replace ("\ n", @ "\ n "). replace ("\ r", @ "\ r "). replace ("\"",@"\""");
 
Pages. controls. add (new System. web. UI. literalControl ("<script language = javascript> alert ('" + msg + "'); </script> "));
}
/** // <Summary>
/// When the form is not loaded, such as pageload, the dialog box is displayed.
/// </Summary>
Public static void Alert_none (string msg)
{
Page pages;
Pages = HttpContext. Current. Handler as System. Web. UI. Page;
Msg = msg. Replace ("'","");
Msg = msg. Replace ("\"","");
Msg = msg. replace ("\ n", @ "\ n "). replace ("\ r", @ "\ r "). replace ("\"",@"\""");
String retu = "alert ('" + msg + "');";
ClientWrite2 (retu );
}
/** // <Summary>
/// Write your own script after loading
/// </Summary>
/// <Param name = "pages"> </param>
/// <Param name = "yourJs"> </param>
Public static void ClientWrite (string yourJs)
{
Page pages;
Pages = HttpContext. Current. Handler as System. Web. UI. Page;
Pages. Controls. Add (new System. Web. UI. LiteralControl ("<script language = javascript>" + yourJs + "</script> "));
}
/** // <Summary>
/// Load the previously written script
/// </Summary>
/// <Param name = "pages"> </param>
/// <Param name = "yourJs"> </param>
Public static void ClientWrite2 (string yourJs)
{
Page pages;
Pages = HttpContext. Current. Handler as System. Web. UI. Page;
Pages. Response. Write ("<script language = javascript> ");
Pages. Response. Write (yourJs );
Pages. Response. Write ("</script> ");
}

/** // <Summary>
/// Obtain the refresh interface string
/// </Summary>
/// <Returns> </returns>
Public static string RefreshWin ()
{
Return "window. location = window. location. href ;";
}
/** // <Summary>
/// Open a small form
/// </Summary>
/// <Param name = "url"> </param>
/// <Param name = "width"> </param>
/// <Param name = "height"> </param>
/// <Param name = "top"> </param>
/// <Param name = "left"> </param>
Public static void OpenLittleWindow (string url, int width, int height, int top, int left)
{
String str;
Str = "javascript: var popup; popup = window. open ('{url}', null, 'scrollbars = yes, status = no, width = {width}, height = {height}, top = {top }, left = {left} '); popup. opener = self. opener; self. close ();";
Str = str. Replace ("{width}", width. ToString ());
Str = str. Replace ("{height}", height. ToString ());
Str = str. Replace ("{top}", top. ToString ());
Str = str. Replace ("{left}", left. ToString ());
Str = str. Replace ("{url}", url );
}

/** // <Summary>
/// Press ENTER-> tab
/// </Summary>
/// <Param name = "page"> </param>
Public static void ToTab ()
{
Page page;
Page = HttpContext. Current. Handler as System. Web. UI. Page;
System. Text. StringBuilder scriptFunction = new StringBuilder ();
ScriptFunction. Append ("<script language = 'javascript '> ");
ScriptFunction. Append ("function returnTotab ()");
ScriptFunction. Append ("{");
ScriptFunction. Append ("if (event. keyCode = 13 )");
ScriptFunction. Append ("{event. keyCode = 9 ;");
ScriptFunction. Append ("return true ;}");
ScriptFunction. Append ("}");
ScriptFunction. Append ("</script> ");
Page. RegisterStartupScript ("totab", scriptFunction. ToString ());

}
/** // <Summary>
/// Tab-> enter
/// </Summary>
/// <Param name = "page"> </param>
Public static void tabToEnter ()
{
Page page;
Page = HttpContext. Current. Handler as System. Web. UI. Page;
System. Text. StringBuilder scriptFunction = new StringBuilder ();
ScriptFunction. Append ("<script language = 'javascript '> ");
ScriptFunction. Append ("function Tcheck ()");
ScriptFunction. Append ("{");
ScriptFunction. Append ("if (event. keyCode = 8 | event. keyCode = 9 )");
ScriptFunction. Append ("return true ;");
ScriptFunction. Append ("else ");
ScriptFunction. Append ("{");
ScriptFunction. append ("if (event. keyCode> = 48) ++ (event. keyCode <= 57) | (event. keyCode> = 96) ++ (event. keyCode <= 105 )))");
ScriptFunction. Append ("return true ;");
ScriptFunction. Append ("else ");
ScriptFunction. append ("if (event. keyCode = 13 | event. keyCode = 110 | event. keyCode = 190 | event. keyCode = 39 )");
ScriptFunction. Append ("{event. keyCode = 9 ;");
ScriptFunction. Append ("return true ;}");
ScriptFunction. Append ("else ");
ScriptFunction. Append ("return false ;");
ScriptFunction. Append ("}");
ScriptFunction. Append ("}");
ScriptFunction. Append ("</script> ");
Page. RegisterStartupScript ("switch", scriptFunction. ToString ());
}
/** // <Summary>
/// AttachEvent
/// </Summary>
/// <Param name = "controlToFocus"> </param>
/// <Param name = "page"> </param>
Public static void attachEvent (Control [] controlToFocus)
{
Page page;
Page = HttpContext. Current. Handler as System. Web. UI. Page;
System. Text. StringBuilder scriptFunction = new StringBuilder ();
String scriptClientId;
ScriptFunction. Append ("<script language = 'javascript '> ");

Foreach (Control con in controlToFocus)
{
ScriptClientId = con. ClientID;
ScriptFunction. Append ("document. getElementById ('" + scriptClientId + "'). attachEvent ('onkeylow', Tcheck );");
}
ScriptFunction. Append ("</script> ");
Page. RegisterStartupScript ("attach", scriptFunction. ToString ());
}
/** // <Summary>
///
/// </Summary>
/// <Param name = "controlToFocus"> </param>
/// <Param name = "page"> </param>
/// <Param name = "eventStr"> </param>
/// <Param name = "FuncStr"> </param>
Public static void AttachEvent (Control [] controlToFocus, string eventStr, string FuncStr)
{
Page page;
Page = HttpContext. Current. Handler as System. Web. UI. Page;
System. Text. StringBuilder scriptFunction = new StringBuilder ();
String scriptClientId;
ScriptFunction. Append ("<script language = 'javascript '> ");
Foreach (Control con in controlToFocus)
{
ScriptClientId = con. ClientID;
ScriptFunction. Append ("document. getElementById ('" + scriptClientId + "'). attachEvent ('" + eventStr + "'," + FuncStr + ");");
}
ScriptFunction. Append ("</script> ");
Page. RegisterStartupScript ("attach2", scriptFunction. ToString ());
}
/** // <Summary>
///
/// </Summary>
/// <Param name = "page"> </param>
Public static void NumOnlyFun ()
{
Page page;
Page = HttpContext. Current. Handler as System. Web. UI. Page;
System. Text. StringBuilder scriptFunction = new StringBuilder ();
ScriptFunction. Append ("<script language = 'javascript '> ");
ScriptFunction. Append ("function isNum ()");
ScriptFunction. Append ("{");
ScriptFunction. Append ("if (event. keyCode = 8 | event. keyCode = 9 )");
ScriptFunction. Append ("return true ;");
ScriptFunction. Append ("else ");
ScriptFunction. Append ("{");
ScriptFunction. append ("if (event. keyCode> = 48) ++ (event. keyCode <= 57) | (event. keyCode> = 96) ++ (event. keyCode <= 105 )))");
ScriptFunction. Append ("return true ;");
ScriptFunction. Append ("else ");
ScriptFunction. Append ("return false ;");
ScriptFunction. Append ("}");
ScriptFunction. Append ("}");
ScriptFunction. Append ("</script> ");
Page. RegisterStartupScript ("numonly", scriptFunction. ToString ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.