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

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

Posted on 2011-05-24 15:39  ☆Keep★Moving☆  阅读(238)  评论(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;

    /// <summary>
    /// 处理一些简单的客户端脚本
    /// </summary>
    public class ClsClientPage
    {

        public static void ComboBoxShowValue(DropDownList cbo, string value)
        {
            try
            {
                ListItem l_objListItem = cbo.Items.FindByValue(value.Trim ());
                if (l_objListItem != null)
                {
                    cbo.SelectedIndex = cbo.Items.IndexOf(l_objListItem);
                    //cbo.SelectedValue = value;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #region ShowMessage

        /// <summary>
        ///ShowMessage
        /// </summary>
        /// <Description> 显示提示信息</Description>
        /// <param name="msg">提示信息</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public static void ShowMessage(System.Web.UI.Page page, string msg)
        {
            if (msg !="")
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
          
        }

        //public static void Show(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);
        //    page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        //}

        /// <summary>
        /// ShowMessage
        /// </summary>
        /// <Description>显示提示信息</Description>
        /// <param name="msg">字符串變量</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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>
        /// ShowModalDialogMessage
        /// </summary>
        /// <Description>在页面加载完毕后显示提示信息</Description>
        /// <param name="msg">字符串變量</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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>
        /// CloseWindow
        /// </summary>
        /// <Description>关闭窗口</Description>
        /// <param name="returnValue">返回值</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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>
        /// CloseModalDialog
        /// </summary>
        /// <Description>关闭模式窗口</Description>
        /// <param name="page">所属页面</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="isRefreshParent">是否刷新父页面</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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.RegisterClientScriptBlock("close_windialog", script);
        }

        /// <summary>
        ///CloseModalDialog
        /// </summary>
        /// <Description> 关闭模式窗口,关闭前可以先显示消息</Description>
        /// <param name="page">所属页面</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="isRefreshParent">是否刷新父页面</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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>
        /// OpenWindow
        /// </summary>
        /// <Description>打开Window</Description>
        /// <param name="url">URL</param>    
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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>
        ///RefreshParent
        /// </summary>
        /// <Description> 通过PostBack某个控件来刷新父窗口,例如PostBack一个LinkButton</Description>
        /// <param name="target">要DoPostBack的目标控件的ID,例如某一Button的ID</param>
        /// <param name="argument">要传回去的参数</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public static void RefreshParent(string target, string argument)
        {
            string script = @"<script language='javascript'>var frmParent;
     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>
        /// RefreshParent
        /// </summary>
        /// <Description>刷新父窗口</Description>
        /// <param></param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public static void RefreshParent()
        {
            string script = @"<script language='javascript'>window.opener.window.navigate(window.opener.window.location.href);</script>";
            System.Web.HttpContext.Current.Response.Write(script);
        }

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

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


        #endregion

        #region WriteScriptToClient
        /// <summary>
        ///WriteScriptToClient
        /// </summary>
        /// <Description> 往前台写脚本</Description>
        /// <param name="script">只写内容不写框架</param>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        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

        /// <summary>
        /// f_CheckSelectedReccordCount
        /// </summary>
        /// <Description>獲取Gridview被選擇的記錄</Description>
        /// <returns>true/false</returns>
        /// <return>none</return>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public  static int f_CheckSelectedReccordCount(GridView l_givObj)
        {
            int l_intCheckedCount = -1;


            for (int i = 0; i <= l_givObj.Rows.Count - 1; i++)
            {
                CheckBox chkSelect = (CheckBox)l_givObj.Rows[i].FindControl("chkSelect");

                if (chkSelect.Checked == true)
                {
                    l_intCheckedCount = l_intCheckedCount + 1;
                }

            }
            return l_intCheckedCount;
        }

        /// <summary>
        /// f_GetSelectedRow
        /// </summary>
        /// <Description>獲取被選擇的行號</Description>
        /// <param name="l_grvObj">gridview</param>
        /// <returns>行號</returns>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public static int f_GetSelectedRow(GridView l_grvObj)
        {
            int l_intCheckedCount = 0;


            for (int i = 0; i <= l_grvObj.Rows.Count - 1; i++)
            {
                CheckBox chkSelect = (CheckBox)l_grvObj.Rows[i].FindControl("chkSelect");

                if (chkSelect.Checked == true)
                {
                    l_intCheckedCount = i;
                }
            }
            return l_intCheckedCount;
        }


        /// <summary>
        /// f_VerifySelectedReccord
        /// </summary>
        /// <Description>顯數據</Description>
        /// <param name="l_givObj">gridview</param>
        /// <returns>檢查信息</returns>
        /// <Name>rainyhuang</Name>
        /// <date>21/09/2008</date>
        public static string  f_VerifySelectedReccord(GridView l_givObj)
        {
            int l_intCheckedCount = -1;
            string l_strMsg="";


            for (int i = 0; i <= l_givObj.Rows.Count - 1; i++)
            {
                CheckBox chkSelect = (CheckBox)l_givObj.Rows[i].FindControl("chkSelect");

                if (chkSelect.Checked == true)
                {
                    l_intCheckedCount = l_intCheckedCount + 1;
                }

            }

            if (l_intCheckedCount < 0)
            {
                l_strMsg = "You didn't select any record";

               // ClsClientPage.ShowMessage(Page, l_strMsg);
               
            }

            if (l_intCheckedCount > 0)
            {

                l_strMsg = "You can only select one record one time";

              //  ClsClientPage.ShowMessage(Page, l_strMsg);
               
            }
            return l_strMsg;
           
        }

        //public static void Export(CrystalDecisions.CrystalReports.Engine.ReportDocument report, CrystalDecisions.Shared.ExportFormatType FileType, string FileName)//使用ExportToStream方式匯出  
        //{

        //    System.IO.Stream stream = report.ExportToStream(FileType);
        //    byte[] bytes = new byte[stream.Length];
        //    stream.Read(bytes, 0, bytes.Length);
        //    stream.Seek(0, System.IO.SeekOrigin.Begin);

        //    //export file  
        //    Response.ClearContent();
        //    Response.ClearHeaders();
        //    Response.AddHeader("content-disposition", "attachment;filename=" + FileName);//excel檔名  

        //    switch (FileType)
        //    {
        //        case CrystalDecisions.Shared.ExportFormatType.Excel:
        //            Response.ContentType = "application/vnd.ms-excel";
        //            break;
        //        case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat:
        //            Response.ContentType = "application/pdf";
        //            break;
        //        case CrystalDecisions.Shared.ExportFormatType.WordForWindows:
        //            Response.ContentType = "application/vnd.ms-word";
        //            break;
        //    }

        //    Response.OutputStream.Write(bytes, 0, bytes.Length);
        //    Response.Flush();
        //    Response.Close();
        //}

        //public static void Export(CrystalDecisions.CrystalReports.Engine.ReportDocument report,CrystalDecisions.Shared.ExportFormatType FileType, string FileName, bool ExportModel)//使用ExportToHttpResponse方式匯出  
        //{
        //    report.ExportToHttpResponse(FileType, Response, ExportModel, FileName);//ExportModel{true:匯出檔案,false:用browse開啓檔案}  
        //}

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    //使用ExportToStream方式匯出  
        //    //Export(CrystalDecisions.Shared.ExportFormatType.WordForWindows, "test.doc");  

        //    //使用ExportToHttpResponse方式匯出  
        //    Export(CrystalDecisions.Shared.ExportFormatType.WordForWindows, "test.doc", true);
        //}
        //protected void Button2_Click(object sender, EventArgs e)
        //{
        //    //使用ExportToStream方式匯出  
        //    //Export(CrystalDecisions.Shared.ExportFormatType.Excel, "test.xls");  

        //    //使用ExportToHttpResponse方式匯出  
        //    Export(CrystalDecisions.Shared.ExportFormatType.Excel, "test.xls", false);
        //}
        //protected void Button3_Click(object sender, EventArgs e)
        //{
        //    //使用ExportToStream方式匯出  
        //    //Export(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, "test.pdf");  

        //    //使用ExportToHttpResponse方式匯出  
        //    Export(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, "test.pdf", true);
        //}  


    }