Common JS Script output classes in ASP. NET

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. Web. UI;
/// <Summary>
/// Some common Js calls
/// Add new version Description: because the old version generally uses the Response. Write (string msg) method to output js scripts, this
/// Js scripts output in the form of html will destroy the entire xhtml structure outside the & lt; html & gt; & lt;/html & gt; tag of the html element,
/// The new version uses ClientScript. RegisterStartupScript (string msg) to output data without changing the xhtml structure,
/// The execution result is not affected.
/// In order to be backward compatible, the new version adopts the overload method. In the new version, an instance of the System. Web. UI. Page class is required.
/// Creation Time: 2006-9-13
/// Creator: Ma xianguang
/// Author of the new version: Zhou Gong
/// Modification date:
// Modified version of the web site: http://blog.csdn.net/zhoufoxcn
/// </Summary>
Public class JScript
{
# Region old version
/// <Summary>
/// A small JavaScript window is displayed.
/// </Summary>
/// <Param name = "js"> window Information </param>
Public static void Alert (string message)
{
# Region
String js = @ "<Script language = 'javascript '>
Alert ('"+ message +"'); </Script> ";
HttpContext. Current. Response. Write (js );
# Endregion
}
/// <Summary>
/// The message box pops up and redirects to the new URL
/// </Summary>
/// <Param name = "message"> message content </param>
/// <Param name = "toURL"> connection address </param>
Public static void AlertAndRedirect (string message, string toURL)
{
# Region
String js = "<script language = javascript> alert ('{0}'); window. location. replace ('{1}') </script> ";
HttpContext. Current. Response. Write (string. Format (js, message, toURL ));
# Endregion
}
/// <Summary>
/// Return to the history page
/// </Summary>
/// <Param name = "value">-1/1 </param>
Public static void GoHistory (int value)
{
# Region
String js = @ "<Script language = 'javascript '>
History. go ({0 });
</Script> ";
HttpContext. Current. Response. Write (string. Format (js, value ));
# Endregion
}
/// <Summary>
/// Close the current window
/// </Summary>
Public static void CloseWindow ()
{
# Region
String js = @ "<Script language = 'javascript '>
Parent. opener = null; window. close ();
</Script> ";
HttpContext. Current. Response. Write (js );
HttpContext. Current. Response. End ();
# Endregion
}
/// <Summary>
/// Refresh the parent window
/// </Summary>
Public static void RefreshParent (string url)
{
# Region
String js = @ "<Script language = 'javascript '>
Window. opener. location. href = '"+ url +"'; window. close (); </Script> ";
HttpContext. Current. Response. Write (js );
# Endregion
}
/// <Summary>
/// Refresh to open the window
/// </Summary>
Public static void RefreshOpener ()
{
# Region
String js = @ "<Script language = 'javascript '>
Opener. location. reload ();
</Script> ";
HttpContext. Current. Response. Write (js );
# Endregion
}
/// <Summary>
/// Open a new form of the specified size
/// </Summary>
/// <Param name = "url"> address </param>
/// <Param name = "width"> width </param>
/// <Param name = "heigth"> high </param>
/// <Param name = "top"> header position </param>
/// <Param name = "left"> left position </param>
Public static void OpenWebFormSize (string url, int width, int heigth, int top, int left)
{
# Region
String js = @ "<Script language = 'javascript '> window. open ('"+ url + @"', '', 'height =" + heigth + ", width =" + width + ", top =" + top + ", left = "+ left +", location = no, menubar = no, resizable = yes, scrollbars = yes, status = yes, titlebar = no, toolbar = no, directories = no'); </Script> ";
HttpContext. Current. Response. Write (js );
# Endregion
}
/// <Summary>
/// Go to the Url-based page
/// </Summary>
/// <Param name = "url"> connection address </param>
Public static void JavaScriptLocationHref (string url)
{
# Region
String js = @ "<Script language = 'javascript '>
Window. location. replace ('{0 }');
</Script> ";
Js = string. Format (js, url );
HttpContext. Current. Response. Write (js );
# Endregion
}
/// <Summary>
/// Open the mode dialog box with the specified size and position
/// </Summary>
/// <Param name = "webFormUrl"> connection address </param>
/// <Param name = "width"> width </param>
/// <Param name = "height"> height </param>
/// <Param name = "top"> distance from top </param>
/// <Param name = "left"> left </param>
Public static void ShowModalDialogWindow (string webFormUrl, int width, int height, int top, int left)
{
# Region
String features = "dialogWidth:" + width. ToString () + "px"
+ "; DialogHeight:" + height. ToString () + "px"
+ "; DialogLeft:" + left. ToString () + "px"
+ "; DialogTop:" + top. ToString () + "px"
+ "; Center: yes; help = no; resizable: no; status: no; scroll = yes ";
ShowModalDialogWindow (webFormUrl, features );
# Endregion
}
/// <Summary>
/// Pop-up modal window
/// </Summary>
/// <Param name = "webFormUrl"> </param>
/// <Param name = "features"> </param>
Public static void ShowModalDialogWindow (string webFormUrl, string features)
{
String js = ShowModalDialogJavascript (webFormUrl, features );
HttpContext. Current. Response. Write (js );
}
/// <Summary>
/// Pop-up modal window
/// </Summary>
/// <Param name = "webFormUrl"> </param>
/// <Param name = "features"> </param>
/// <Returns> </returns>
Public static string ShowModalDialogJavascript (string webFormUrl, string features)
{
# Region
String js = @ "<script language = javascript>
ShowModalDialog ('"+ webFormUrl +"', '', '" + features + "'); </script> ";
Return js;
# Endregion
}
# Endregion
# New version of region
/// <Summary>
/// A small JavaScript window is displayed.
/// </Summary>
/// <Param name = "js"> window Information </param>
Public static void Alert (string message, Page page)
{
# Region
String js = @ "<Script language = 'javascript '>
Alert ('"+ message +"'); </Script> ";
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "alert "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "alert", js );
}
# Endregion
}
/// <Summary>
/// The message box pops up and redirects to the new URL
/// </Summary>
/// <Param name = "message"> message content </param>
/// <Param name = "toURL"> connection address </param>
Public static void AlertAndRedirect (string message, string toURL, Page page)
{
# Region
String js = "<script language = javascript> alert ('{0}'); window. location. replace ('{1}') </script> ";
// HttpContext. Current. Response. Write (string. Format (js, message, toURL ));
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "AlertAndRedirect "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "AlertAndRedirect", string. Format (js, message, toURL ));
}
# Endregion
}
/// <Summary>
/// Return to the history page
/// </Summary>
/// <Param name = "value">-1/1 </param>
Public static void GoHistory (int value, Page page)
{
# Region
String js = @ "<Script language = 'javascript '>
History. go ({0 });
</Script> ";
// HttpContext. Current. Response. Write (string. Format (js, value ));
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "GoHistory "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "GoHistory", string. Format (js, value ));
}
# Endregion
}
///// <Summary>
//// Close the current window
///// </Summary>
// Public static void CloseWindow ()
//{
// # Region
// String js = @ "<Script language = 'javascript '>
// Parent. opener = null; window. close ();
// </Script> ";
// HttpContext. Current. Response. Write (js );
// HttpContext. Current. Response. End ();
// # Endregion
//}
/// <Summary>
/// Refresh the parent window
/// </Summary>
Public static void RefreshParent (string url, Page page)
{
# Region
String js = @ "<Script language = 'javascript '>
Window. opener. location. href = '"+ url +"'; window. close (); </Script> ";
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "RefreshParent "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "RefreshParent", js );
}
# Endregion
}
/// <Summary>
/// Refresh to open the window
/// </Summary>
Public static void RefreshOpener (Page page)
{
# Region
String js = @ "<Script language = 'javascript '>
Opener. location. reload ();
</Script> ";
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "RefreshOpener "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "RefreshOpener", js );
}
# Endregion
}
/// <Summary>
/// Open a new form of the specified size
/// </Summary>
/// <Param name = "url"> address </param>
/// <Param name = "width"> width </param>
/// <Param name = "heigth"> high </param>
/// <Param name = "top"> header position </param>
/// <Param name = "left"> left position </param>
Public static void OpenWebFormSize (string url, int width, int heigth, int top, int left, Page)
{
# Region
String js = @ "<Script language = 'javascript '> window. open ('"+ url + @"', '', 'height =" + heigth + ", width =" + width + ", top =" + top + ", left = "+ left +", location = no, menubar = no, resizable = yes, scrollbars = yes, status = yes, titlebar = no, toolbar = no, directories = no'); </Script> ";
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "OpenWebFormSize "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "OpenWebFormSize", js );
}
# Endregion
}
/// <Summary>
/// Go to the Url-based page
/// </Summary>
/// <Param name = "url"> connection address </param>
Public static void JavaScriptLocationHref (string url, Page page)
{
# Region
String js = @ "<Script language = 'javascript '>
Window. location. replace ('{0 }');
</Script> ";
Js = string. Format (js, url );
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "JavaScriptLocationHref "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "JavaScriptLocationHref", js );
}
# Endregion
}
/// <Summary>
/// Open the mode dialog box with the specified size and position
/// </Summary>
/// <Param name = "webFormUrl"> connection address </param>
/// <Param name = "width"> width </param>
/// <Param name = "height"> height </param>
/// <Param name = "top"> distance from top </param>
/// <Param name = "left"> left </param>
Public static void ShowModalDialogWindow (string webFormUrl, int width, int height, int top, int left, Page page)
{
# Region
String features = "dialogWidth:" + width. ToString () + "px"
+ "; DialogHeight:" + height. ToString () + "px"
+ "; DialogLeft:" + left. ToString () + "px"
+ "; DialogTop:" + top. ToString () + "px"
+ "; Center: yes; help = no; resizable: no; status: no; scroll = yes ";
ShowModalDialogWindow (webFormUrl, features, page );
# Endregion
}
/// <Summary>
/// Pop-up modal window
/// </Summary>
/// <Param name = "webFormUrl"> </param>
/// <Param name = "features"> </param>
Public static void ShowModalDialogWindow (string webFormUrl, string features, Page page)
{
String js = ShowModalDialogJavascript (webFormUrl, features );
// HttpContext. Current. Response. Write (js );
If (! Page. ClientScript. IsStartupScriptRegistered (page. GetType (), "ShowModalDialogWindow "))
{
Page. ClientScript. RegisterStartupScript (page. GetType (), "ShowModalDialogWindow", js );
}
}
///// <Summary>
//// The modal window is displayed.
///// </Summary>
///// <Param name = "webFormUrl"> </param>
///// <Param name = "features"> </param>
///// <Returns> </returns>
// Public static string ShowModalDialogJavascript (string webFormUrl, string features)
//{
// # Region
// String js = @ "<script language = javascript>
// ShowModalDialog ('"+ webFormUrl +"', '', '" + features + "'); </script> ";
// Return js;
// # Endregion
//}
# Endregion

Source

 

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.