When writing ASP. NET programs, some JS scripts similar to alert are often used. You can write a class of common methods in a set for future use.
Using system. Web;
/// <Summary>
/// Common Javascript methods
/// </Summary>
Public class JS
{
Private Static string scriptstart = "<SCRIPT type = \" text/JavaScript \ "> ";
Private Static string scriptend = "</SCRIPT> ";
/// <Summary>
/// Write the JS script content
/// </Summary>
/// <Param name = "scriptstring"> script content </param>
/// <Param name = "isresponseend"> whether to interrupt server script execution </param>
Public static void writescript (string scriptstring, bool isresponseend)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write (scriptstring );
Httpcontext. Current. response. Write (scriptend );
If (isresponseend)
{
Httpcontext. Current. response. End ();
}
}
/// <Summary>
/// A warning box is displayed.
/// </Summary>
/// <Param name = "alertmessage"> message </param>
/// <Param name = "isresponseend"> whether to interrupt server script execution </param>
Public static void alert (string alertmessage, bool isresponseend)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write ("alert ('" + alertmessage + "'); history. Back ();");
Httpcontext. Current. response. Write (scriptend );
If (isresponseend)
{
Httpcontext. Current. response. End ();
}
}
/// <Summary>
/// Pop up the warning box and jump
/// </Summary>
/// <Param name = "alertmessage"> message </param>
/// <Param name = "redirectpath"> jump path </param>
/// <Param name = "istopwindow"> whether to jump to full screen </param>
Public static void alert (string alertmessage, string redirectpath, bool istopwindow)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write ("alert ('" + alertmessage + "');");
If (istopwindow)
{
Httpcontext. Current. response. Write ("parent. Top. Location. href = '" + redirectpath + "';");
}
Else
{
Httpcontext. Current. response. Write ("location. href = '" + redirectpath + "';");
}
Httpcontext. Current. response. Write (scriptend );
Httpcontext. Current. response. End ();
}
/// <Summary>
/// A warning box is displayed and the window is closed.
/// </Summary>
/// <Param name = "alertmessage"> message </param>
Public static void alertandclose (string alertmessage)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write ("alert ('" + alertmessage + "'); window. Close ();");
Httpcontext. Current. response. Write (scriptend );
Httpcontext. Current. response. End ();
}
/// <Summary>
/// Full screen jump
/// </Summary>
/// <Param name = "redirectppath"> jump path </param>
Public static void topredirect (string redirectppath)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write ("parent. Top. Location. href = '" + redirectppath + "';");
Httpcontext. Current. response. Write (scriptend );
Httpcontext. Current. response. End ();
}
/// <Summary>
/// Determine and jump
/// </Summary>
/// <Param name = "confirmmessage"> message </param>
/// <Param name = "yesredirectpath"> select "yes" and the jump path </param>
/// <Param name = "noredirectpath"> select "no" and the jump path </param>
/// <Param name = "isresponseend"> whether to interrupt server script execution </param>
Public static void confirmredirect (string confirmmessage, string yesredirectpath, string noredirectpath, bool isresponseend)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. current. response. write ("If (confirm ('" + confirmmessage + "') {location. href = '"+ yesredirectpath +"';} else {location. href = '"+ noredirectpath + "';}");
Httpcontext. Current. response. Write (scriptend );
If (isresponseend)
{
Httpcontext. Current. response. End ();
}
}
/// <Summary>
/// Judge and write the script code
/// </Summary>
/// <Param name = "confirmmessage"> message </param>
/// <Param name = "yesscript"> select "yes" and write the script content </param>
/// <Param name = "NoScript"> select "no" and write the script content </param>
/// <Param name = "isresponseend"> whether to interrupt server script execution </param>
Public static void confirmscript (string confirmmessage, string yesscript, string NoScript, bool isresponseend)
{
Httpcontext. Current. response. Write (scriptstart );
Httpcontext. Current. response. Write ("If (confirm ('" + confirmmessage + "') {" + yesscript + "} else {" + NoScript + "}");
Httpcontext. Current. response. Write (scriptend );
If (isresponseend)
{
Httpcontext. Current. response. End ();
}
}
}