博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

处理一些简单的客户端脚本

Posted on 2011-05-24 15:38  ☆Keep★Moving☆  阅读(231)  评论(0编辑  收藏  举报

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// 处理一些简单的客户端脚本
/// </summary>
public class ClientHelper
{
    #region ShowMessage

    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="msg">提示信息</param>
    public static void ShowMessage(System.Web.UI.Page page, string msg)
    {
        string script = "<script language = 'javascript'>alert('{0}');</script>";
        if (msg.IndexOf("'") >= 0)
        {
            script = "<script language = 'javascript'>alert(\"{0}\");</script>";
        }
        script = string.Format(script, msg);
        page.RegisterClientScriptBlock("showMsg", script);
    }

    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="msg">提示信息</param>
    public static void ShowMessage(String msg)
    {
        string script = "<script language = 'javascript'>alert('{0}');</script>";
        if (msg.IndexOf("'") >= 0)
        {
            script = "<script language = 'javascript'>alert(\"{0}\");</script>";
        }
        script = string.Format(script, msg);
        System.Web.HttpContext.Current.Response.Write(script);
    }

    public static void OpenModalWindow(string url, string style)
    {
        string script = "<script language = 'javascript'>window.showModalDialog(encodeURI('{0}'),null,'{1}');</script>";
        script = string.Format(script, url, style);
        System.Web.HttpContext.Current.Response.Write(script);
    }


    /// <summary>
    /// 在页面加载完毕后显示提示信息
    /// </summary>
    /// <param name="msg">提示信息</param>
    public static void ShowModalDialogMessage(System.Web.UI.Page page, string msg)
    {
        string script = "<script language = 'javascript'>alert('{0}');</script>";
        if (msg.IndexOf("'") >= 0)
        {
            script = "<script language = 'javascript'>alert(\"{0}\");</script>";
        }
        script = string.Format(script, msg);
        page.RegisterStartupScript("showMsg", script);
    }

    #endregion

    #region CloseWindow

    /// <summary>
    /// 关闭窗口
    /// </summary>
    /// <param name="returnValue">返回值</param>
    public static void CloseWindow(string returnValue)
    {
        string script = "<script language = 'javascript'>{0}window.close();</script>";
        if (returnValue != "")
        {
            script = string.Format(script, string.Format("window.returnValue = '{0}';", returnValue));
        }
        else
        {
            script = string.Format(script, "");
        }
        System.Web.HttpContext.Current.Response.Write(script);
    }
    /// <summary>
    /// 关闭窗口
    /// </summary>
    public static void CloseWindow()
    {
        CloseWindow("");
    }

    /// <summary>
    /// 关闭模式窗口
    /// </summary>
    /// <param name="page">所属页面</param>
    /// <param name="returnValue">返回值</param>
    /// <param name="isRefreshParent">是否刷新父页面</param>
    public static void CloseModalDialog(System.Web.UI.Page page, string returnValue, bool isRefreshParent)
    {
        string script = "<script language='javascript'> ";
        if (isRefreshParent)
            script += "window.parent.dialogArguments.document.location.href=window.parent.dialogArguments.document.location.href;";
        if (returnValue != "")
            script += " window.returnValue='" + returnValue + "';";
        script += " window.close(true);</script>";
        page.RegisterStartupScript("close_windialog", script);
    }

    /// <summary>
    /// 关闭模式窗口,关闭前可以先显示消息
    /// </summary>
    /// <param name="page">所属页面</param>
    /// <param name="returnValue">返回值</param>
    /// <param name="isRefreshParent">是否刷新父页面</param>
    public static void CloseModalDialog(System.Web.UI.Page page, string returnValue, bool isRefreshParent, string MsgShowBeforeClose)
    {
        string script = "<script language='javascript'> ";
        if (MsgShowBeforeClose.Length > 0)
            script += "alert('" + MsgShowBeforeClose + "');";
        if (isRefreshParent)
            script += "window.parent.dialogArguments.document.location.href=window.parent.dialogArguments.document.location.href;";
        if (returnValue != "")
            script += " window.returnValue='" + returnValue + "';";
        script += " window.close(true);</script>";
        page.RegisterStartupScript("close_windialog", script);
    }

    #endregion

    #region OpenWindow
    /// <summary>
    /// 打开Window
    /// </summary>
    /// <param name="url">URL</param>
    public static void OpenWindow(string url)
    {
        string script = "<script language = 'javascript'>window.open(encodeURI('{0}'));</script>";
        script = string.Format(script, url);
        System.Web.HttpContext.Current.Response.Write(script);
    }
    #endregion

    #region Refresh Parent Window

    /// <summary>
    /// 通过PostBack某个控件来刷新父窗口,例如PostBack一个LinkButton
    /// </summary>
    /// <param name="target">要DoPostBack的目标控件的ID,例如某一Button的ID</param>
    /// <param name="argument">要传回去的参数</param>
    public static void RefreshParent(string target, string argument)
    {
        string script = @"<script language='javascript'>var frmParent;window.parent.returnValue='t';
     if (window.navigator.appName.toLowerCase().indexOf('netscape') > -1)
     {
      frmParent = window.opener.window.document.forms['Form1'];
     }
     else
     {
      frmParent = window.opener.window.document.Form1;
     }
     frmParent.__EVENTTARGET.value = '{@Target}';
     frmParent.__EVENTARGUMENT.value = '{@Argument}';
     frmParent.submit();</script>";
        script = script.Replace("{@Target}", target).Replace("{@Argument}", argument);
        System.Web.HttpContext.Current.Response.Write(script);
    }
    /// <summary>
    /// 刷新父窗口
    /// </summary>
    public static void RefreshParent()
    {
        string script = @"<script language='javascript'>window.parent.returnValue='t'; window.opener.window.navigate(window.opener.window.location.href);</script>";
        System.Web.HttpContext.Current.Response.Write(script);
    }

    /// <summary>
    /// 刷新模式窗口的父窗口
    /// </summary>
    /// <param name="page">所属页面</param>
    /// <param name="clientScriptKey">脚本在客户端的key</param>
    /// <param name="isClose">是否关闭当前页面</param>
    public static void RefreshModalDialogParent(System.Web.UI.Page page, string clientScriptKey, bool isClose)
    {
        string script = "<script language='javascript'>window.parent.returnValue='t'; window.parent.dialogArguments.document.location.reload();";

        if (isClose)
            script += "window.close(false);</script>";
        else
            script += "</script>";
        page.RegisterStartupScript(clientScriptKey, script);
    }


    #endregion

    #region WriteScriptToClient
    /// <summary>
    /// 往前台写脚本
    /// </summary>
    /// <param name="script">只写内容不写框架</param>
    public static void WriteScriptToClient(System.Web.UI.Page page, String script)
    {
        string tmpScript = String.Format("<script language = 'javascript'>{0}</script>", script);
        page.RegisterClientScriptBlock("showMsg", tmpScript);
    }
    #endregion

   
}