Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Class method tutorial for outputting JS scripts commonly used in ASP.NET

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the "ASP.NET commonly used to output JS scripts class method tutorial" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

The details are as follows:

Very often in ASP.NET, we often need to output some JS scripts, such as a pop-up warning window, return to the history page and other JS functions. I see that what is widely spread on the Internet is a JScript class written by Ma Xianguang, which basically contains the frequently used JS script, which is very convenient. The only deficiency is the Response.Write (string msg) method adopted by the author, which results in the output js script outside the tag. Destroy the structure of the original XHTML, so I made further improvements to the JScript class to meet the original function. This improvement adopts the method of overloading and adds an instance of the System.Web.UI.Page class as a parameter, which will not affect the original program code.

The code of the whole program is as follows:

The copy code is as follows:

Using System

Using System.Collections.Generic

Using System.Text

Using System.Web

Using System.Web.UI

/ / /

/ / some commonly used Js calls

/ add a new version of note: because the old version generally uses Response.Write (string msg) to output js scripts, this

The js script output in the / mode destroys the structure of the entire xhtml outside the tag of the html element

/ / the new version is outputted by ClientScript.RegisterStartupScript (string msg), which will not change the structure of xhtml

/ / will not affect the execution effect.

/ / for backward compatibility, the new version is overloaded and requires an instance of the System.Web.UI.Page class.

/ / creator: Ma Xianguang

/ / author of the new edition: Duke Zhou

/ / modified version release URL: http://blog.csdn.net/zhoufoxcn

/ / /

Public class JScript

{

# Old version of region

/ / /

/ / the JavaScript mini window pops up

/ / /

/ / window information

Public static void Alert (string message)

{

# region

String js = @ "

Alert ('"+ message +"); "

HttpContext.Current.Response.Write (js)

# endregion

}

/ / /

/ / pops up the message box and goes to the new URL

/ / /

/ / message content

/ / connection address

Public static void AlertAndRedirect (string message, string toURL)

{

# region

String js = "alert ('{0}'); _ window.location.replace ('{1}')"

HttpContext.Current.Response.Write (string.Format (js, message, toURL))

# endregion

}

/ / /

/ / go back to the history page

/ / /

/-1Compact 1

Public static void GoHistory (int value)

{

# region

String js = @ "

History.go ({0})

"

HttpContext.Current.Response.Write (string.Format (js, value))

# endregion

}

/ / /

/ / close the current window

/ / /

Public static void CloseWindow ()

{

# region

String js = @ "

Parent.opener=null;window.close ()

"

HttpContext.Current.Response.Write (js)

HttpContext.Current.Response.End ()

# endregion

}

/ / /

/ / refresh the parent window

/ / /

Public static void RefreshParent (string url)

{

# region

String js = @ "

Window.opener.location.href=' "+ url +"; window.close ();

HttpContext.Current.Response.Write (js)

# endregion

}

/ / /

/ Refresh the open window

/ / /

Public static void RefreshOpener ()

{

# region

String js = @ "

Opener.location.reload ()

"

HttpContext.Current.Response.Write (js)

# endregion

}

/ / /

/ / Open a new form of specified size

/ / /

/ / address

/ / wide

/ / High

/ / head position

/ / left position

Public static void OpenWebFormSize (string url, int width, int heigth, int top, int left)

{

# region

String js = @ "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');"

HttpContext.Current.Response.Write (js)

# endregion

}

/ / /

/ / go to the page developed by Url

/ / /

/ / connection address

Public static void JavaScriptLocationHref (string url)

{

# region

String js = @ "

_ window.location.replace ('{0}')

"

Js = string.Format (js, url)

HttpContext.Current.Response.Write (js)

# endregion

}

/ / /

/ / Open the mode dialog box for the specified size and location

/ / /

/ / connection address

/ / wide

/ / High

/ / position at distance

/ / distance to the left

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

}

/ / /

/ / pop-up mode window

/ / /

/ / /

/ / /

Public static void ShowModalDialogWindow (string webFormUrl, string features)

{

String js = ShowModalDialogJavascript (webFormUrl, features)

HttpContext.Current.Response.Write (js)

}

/ / /

/ / pop-up mode window

/ / /

/ / /

/ / /

/ / /

Public static string ShowModalDialogJavascript (string webFormUrl, string features)

{

# region

String js = @ "

ShowModalDialog ('"+ webFormUrl +",',', "+ features +"); "

Return js

# endregion

}

# endregion

# New version of region

/ / /

/ / the JavaScript mini window pops up

/ / /

/ / window information

Public static void Alert (string message, Page page)

{

# region

String js = @ "

Alert ('"+ message +"); "

/ / HttpContext.Current.Response.Write (js)

If (! page.ClientScript.IsStartupScriptRegistered (page.GetType (), "alert"))

{

Page.ClientScript.RegisterStartupScript (page.GetType (), "alert", js)

}

# endregion

}

/ / /

/ / pops up the message box and goes to the new URL

/ / /

/ / message content

/ / connection address

Public static void AlertAndRedirect (string message, string toURL, Page page)

{

# region

String js = "alert ('{0}'); _ window.location.replace ('{1}')"

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

}

/ / /

/ / go back to the history page

/ / /

/-1Compact 1

Public static void GoHistory (int value, Page page)

{

# region

String js = @ "

History.go ({0})

"

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

}

/

/ / close the current window

/

/ / public static void CloseWindow ()

/ / {

/ / # region

/ / string js = @ "

/ / parent.opener=null;window.close ()

/ / "

/ / HttpContext.Current.Response.Write (js)

/ / HttpContext.Current.Response.End ()

/ / # endregion

/ /}

/ / /

/ / refresh the parent window

/ / /

Public static void RefreshParent (string url, Page page)

{

# region

String js = @ "

Window.opener.location.href=' "+ url +"; window.close ();

/ / HttpContext.Current.Response.Write (js)

If (! page.ClientScript.IsStartupScriptRegistered (page.GetType (), "RefreshParent"))

{

Page.ClientScript.RegisterStartupScript (page.GetType (), "RefreshParent", js)

}

# endregion

}

/ / /

/ Refresh the open window

/ / /

Public static void RefreshOpener (Page page)

{

# region

String js = @ "

Opener.location.reload ()

"

/ / HttpContext.Current.Response.Write (js)

If (! page.ClientScript.IsStartupScriptRegistered (page.GetType (), "RefreshOpener"))

{

Page.ClientScript.RegisterStartupScript (page.GetType (), "RefreshOpener", js)

}

# endregion

}

/ / /

/ / Open a new form of specified size

/ / /

/ / address

/ / wide

/ / High

/ / head position

/ / left position

Public static void OpenWebFormSize (string url, int width, int heigth, int top, int left, Page page)

{

# region

String js = @ "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');"

/ / HttpContext.Current.Response.Write (js)

If (! page.ClientScript.IsStartupScriptRegistered (page.GetType (), "OpenWebFormSize"))

{

Page.ClientScript.RegisterStartupScript (page.GetType (), "OpenWebFormSize", js)

}

# endregion

}

/ / /

/ / go to the page developed by Url

/ / /

/ / connection address

Public static void JavaScriptLocationHref (string url, Page page)

{

# region

String js = @ "

_ window.location.replace ('{0}')

"

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

}

/ / /

/ / Open the mode dialog box for the specified size and location

/ / /

/ / connection address

/ / wide

/ / High

/ / position at distance

/ / distance to the left

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

}

/ / /

/ / pop-up mode window

/ / /

/ / /

/ / /

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)

}

}

/

/ / pop-up mode window

/

/

/

/

/ / public static string ShowModalDialogJavascript (string webFormUrl, string features)

/ / {

/ / # region

/ / string js = @ "

/ / showModalDialog ('"+ webFormUrl +",'','"+ features +"'); "

/ / return js

/ / # endregion

/ /}

# endregion

}

This is the end of the tutorial on Class methods commonly used to output JS scripts in ASP.NET. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report