功能很强大的UI封装类

来源:http://www.cnblogs.com/chu888chu888/archive/2008/02/03/1063266.html

  1/******************************************************************
   2** Copyright (c) 2001-2006 楚广明
   3** FileName:    UIHelper.cs
   4** Creator:     改版楚广明    
   5** CreateDate:  2006/05/06
   6** Changer:     2007/03/01将以前的所有静态方法都改为实例化方化
   7**              此次变动有例于以后的方法重载
   8** LastChangeDate:
   9** Description: Class used for UI unification.
  10**              It will make easier to set the UI. 
  11**              More info please read the remark of functions.
  12** VersionInfo:
  13******************************************************************/

  14using System;
  15using System.Data;
  16using System.Configuration;
  17using System.Web;
  18using System.Web.Security;
  19using System.Web.UI;
  20using System.Web.UI.WebControls;
  21using System.Web.UI.WebControls.WebParts;
  22using System.Web.UI.HtmlControls;
  23using System.Drawing;
  24using System.Text;
  25using System.IO;
  26using System.Threading;
  27namespace WebHelper.UI
  28{
  29    /// <summary>
  30    /// Summary description for UIHelper
  31    /// </summary>

  32    public static class UIHelper
  33    {
  34        #region  Static Property Get BaseUrl(静态属性获取URL地址)
  35        /// <summary>
  36        /// 这个静态属性的调用必须用以下代码方法调用
  37        /// 代码调用:
  38        /// Response.Write(UIHelper.BaseUrl);
  39        /// </summary>

  40        public static string BaseUrl
  41        {
  42            get
  43            {
  44                //strBaseUrl用于存储URL地址
  45                string strBaseUrl = "";
  46                //获取当前HttpContext下的地址
  47                strBaseUrl += "http://" + HttpContext.Current.Request.Url.Host;
  48                //如果端口不是80的话,那么加入特殊端口
  49                if (HttpContext.Current.Request.Url.Port.ToString() != "80")
  50                {
  51                    strBaseUrl += ":" + HttpContext.Current.Request.Url.Port;
  52                }

  53                strBaseUrl += HttpContext.Current.Request.ApplicationPath;
  54
  55                return strBaseUrl + "/";
  56            }

  57        }

  58        #endregion

  59
  60        #region  Alert()
  61        /// <summary>
  62        /// 简单弹出对话框功能
  63        /// 代码调用:
  64        /// UIHelper.Alert(this.Page,"OKOK");
  65        /// 
  66        /// 
  67        /// </summary>
  68        /// <param name="pageCurrent">
  69        /// 当前的页面
  70        /// </param>
  71        /// <param name="strMsg">
  72        /// 弹出信息的内容
  73        /// </param>

  74        public static void Alert(System.Web.UI.Page pageCurrent, string strMsg)
  75        {
  76            //Replace /n
  77            strMsg = strMsg.Replace("/n""file://n/");
  78            //Replace /r
  79            strMsg = strMsg.Replace("/r""file://r/");
  80            //Replace "
  81            strMsg = strMsg.Replace("/"""///"");
  82            //Replace '
  83            strMsg = strMsg.Replace("/'""///'");
  84
  85            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
  86                System.Guid.NewGuid().ToString(),
  87                "<script>window.alert('" + strMsg + "')</script>"
  88                );
  89
  90            //以下代码是兼容.net1.1版本的,但到了2.0时代此API就过时了
  91            //pageCurrent.RegisterStartupScript(
  92            //    System.Guid.NewGuid().ToString(),
  93            //    "<script>window.alert('" + strMsg + "')</script>"
  94            //    );
  95        }

  96        public static void Alert(System.Web.UI.Page pageCurrent, string strMsg, string GoBackUrl)
  97        {
  98            //Replace /n
  99            strMsg = strMsg.Replace("/n""file://n/");
 100            //Replace /r
 101            strMsg = strMsg.Replace("/r""file://r/");
 102            //Replace "
 103            strMsg = strMsg.Replace("/"""///"");
 104            //Replace '
 105            strMsg = strMsg.Replace("/'""///'");
 106
 107            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 108                System.Guid.NewGuid().ToString(),
 109                "<script>window.alert('" + strMsg + "');location='" + GoBackUrl + "'</script>"
 110                );
 111
 112            //以下代码是兼容.net1.1版本的,但到了2.0时代此API就过时了
 113            //pageCurrent.RegisterStartupScript(
 114            //    System.Guid.NewGuid().ToString(),
 115            //    "<script>window.alert('" + strMsg + "')</script>"
 116            //    );
 117        }

 118
 119
 120        #endregion

 121
 122        #region ScrollMessage
 123        /// <summary>
 124        /// 简单的滚动信息栏
 125        /// 代码调用
 126        ///         UIHelper.ScrollMessage(this.Page, "滚动的内容");
 127        /// </summary>
 128        /// <param name="pageCurrent">
 129        /// 当前页面
 130        /// </param>
 131        /// <param name="strMsg">
 132        /// 要滚动的信息
 133        /// </param>

 134        public static string ScrollMessage(string strMsg)
 135        {
 136            //Replace /n
 137            strMsg = strMsg.Replace("/n""file://n/");
 138            //Replace /r
 139            strMsg = strMsg.Replace("/r""file://r/");
 140            //Replace "
 141            strMsg = strMsg.Replace("/"""///"");
 142            //Replace '
 143            strMsg = strMsg.Replace("/'""///'");
 144
 145
 146            StringBuilder sb = new StringBuilder();
 147            sb.Append("<MARQUEE>");
 148            sb.Append(strMsg);
 149            sb.Append("</MARQUEE>");
 150            return sb.ToString();
 151            //pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 152            //        System.Guid.NewGuid().ToString(),sb.ToString());
 153        }

 154
 155
 156        /// <summary>
 157        /// 指定滚动文字的详细方法
 158        /// 
 159        /// </summary>
 160        /// <param name="pageCurrent">
 161        /// 当前的页面
 162        /// </param>
 163        /// <param name="strMsg">
 164        /// 要滚动的文字
 165        /// </param>
 166        /// <param name="aligh">
 167        /// align:是设定活动字幕的位置,居左、居中、居右、靠上和靠下三种位置
 168        /// left center  right top bottom 
 169        /// </param>
 170        /// <param name="bgcolor">
 171        /// 用于设定活动字幕的背景颜色,一般是十六进制数。如#CCCCCC 
 172        /// </param>
 173        /// <param name="direction">
 174        /// 用于设定活动字幕的滚动方向是向左、向右、向上、向下
 175        /// left|right|up|down 
 176        /// </param>
 177        /// <param name="behavior">
 178        /// 用于设定滚动的方式,主要由三种方式:scroll slide和alternate
 179        /// 
 180        /// </param>
 181        /// <param name="height">
 182        /// 用于设定滚动字幕的高度
 183        /// 
 184        /// </param>
 185        /// <param name="hspace">
 186        /// 则设定滚动字幕的宽度
 187        /// </param>
 188        /// <param name="scrollamount">
 189        /// 用于设定活动字幕的滚动距离
 190        /// </param>
 191        /// <param name="scrolldelay">
 192        /// 用于设定滚动两次之间的延迟时间
 193        /// </param>
 194        /// <param name="width"></param>
 195        /// <param name="vspace">
 196        /// 分别用于设定滚动字幕的左右边框和上下边框的宽度
 197        /// 
 198        /// </param>
 199        /// <param name="loop">
 200        /// 用于设定滚动的次数,当loop=-1表示一直滚动下去,直到页面更新
 201        /// </param>
 202        /// <param name="MarqueejavascriptPath">
 203        /// 脚本的存放位置
 204        /// </param>
 205        /// <returns></returns>

 206        public static string ScrollMessage(System.Web.UI.Page pageCurrent, string strMsg, string aligh, string bgcolor,
 207                                    string direction, string behavior, string height, string hspace,
 208                                    string scrollamount, string scrolldelay, string width, string vspace, string loop,
 209                                    string MarqueejavascriptPath)
 210        {
 211            StreamReader sr = new StreamReader(pageCurrent.MapPath(MarqueejavascriptPath));
 212            StringBuilder sb = new StringBuilder();
 213            string line;
 214            try
 215            {
 216                while ((line = sr.ReadLine()) != null)
 217                {
 218                    sb.AppendLine(line);
 219
 220                }

 221                sr.Close();
 222            }

 223            catch (Exception ex)
 224            {
 225                throw new Exception(ex.Message);
 226            }

 227            sb.Replace("$strMessage", strMsg);
 228            sb.Replace("$aligh", aligh);
 229            sb.Replace("$bgcolor", bgcolor);
 230            sb.Replace("$direction", direction);
 231            sb.Replace("$behavior", behavior);
 232            sb.Replace("$height", height);
 233            sb.Replace("$hspace", hspace);
 234            sb.Replace("$scrollamount", scrollamount);
 235            sb.Replace("$scrolldelay", scrolldelay);
 236            sb.Replace("$width", width);
 237            sb.Replace("$vspace", vspace);
 238            sb.Replace("$loop", loop);
 239            //pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 240            //            System.Guid.NewGuid().ToString(), sb.ToString());
 241            return sb.ToString();
 242        }

 243
 244        #endregion

 245
 246        #region  Redirect()
 247        /// <summary>
 248        /// Add the javascript method to redirect page on client
 249        /// Created : Wang Hui, May 18,2006
 250        /// Modified: 
 251        /// </summary>
 252        /// <param name="pageCurrent"></param>
 253        /// <param name="strPage"></param>

 254        public static void Redirect(System.Web.UI.Page pageCurrent, string strPage)
 255        {
 256            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 257                    System.Guid.NewGuid().ToString(),
 258                    "<script>window.location.href='" + strPage + "'</script>"
 259                    );
 260
 261            //以下方法是兼容1.1的,2.0过时
 262            //pageCurrent.RegisterStartupScript(
 263            //    System.Guid.NewGuid().ToString(),
 264            //    "<script>window.location.href='" + strPage + "'</script>"
 265            //    );
 266        }

 267        /// <summary>
 268        /// 主要用于跳出带有框架的页面
 269        /// </summary>
 270        /// <param name="pageCurrent">当前页面如this.page</param>
 271        /// <param name="strPage">要跳出的页面</param>

 272        public static void RedirectFrame(System.Web.UI.Page pageCurrent, string strPage)
 273        {
 274            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 275                    System.Guid.NewGuid().ToString(),
 276                    "<script>window.top.location.href='" + strPage + "'</script>"
 277                    );
 278
 279            //以下方法是兼容1.1的,2.0过时
 280            //pageCurrent.RegisterStartupScript(
 281            //    System.Guid.NewGuid().ToString(),
 282            //    "<script>window.location.href='" + strPage + "'</script>"
 283            //    );
 284        }

 285
 286        #endregion

 287
 288        #region AddConfirm
 289        /// <summary>
 290        /// Add the confirm message to button
 291        /// Created : GuangMing Chu 1,1,2007
 292        /// Modified: GuangMing Chu 1,1,2007
 293        /// Modified: GuangMing Chu 1,1,2007
 294        /// 代码调用:
 295        ///    UIHelper.AddConfirm(this.Button1, "真的要删了??");
 296        /// 点确定按钮就会执行事件中的代码,点取消不会
 297        /// </summary>
 298        /// <param name="button">The control, must be a button</param>
 299        /// <param name="strMsg">The popup message</param>

 300        public static void AddConfirm(System.Web.UI.WebControls.Button button, string strMsg)
 301        {
 302            strMsg = strMsg.Replace("/n""file://n/");
 303            strMsg = strMsg.Replace("/r""file://r/");
 304            strMsg = strMsg.Replace("/"""///"");
 305            strMsg = strMsg.Replace("/'""///'");
 306            button.Attributes.Add("onClick""return confirm('" + strMsg + "')");
 307        }

 308
 309        /// <summary>
 310        /// Add the confirm message to button
 311        /// Created : GuangMing Chu, 1 1,2007
 312        /// Modified: GuangMing Chu, 1 1,2007
 313        /// Modified:
 314        /// 代码调用:
 315        ///       UIHelper.AddConfirm(this.Button1, "真的要删了??");
 316        /// 点确定按钮就会执行事件中的代码,点取消不会
 317        ///      
 318        /// </summary>
 319        /// <param name="button">The control, must be a button</param>
 320        /// <param name="strMsg">The popup message</param>

 321        public static void AddConfirm(System.Web.UI.WebControls.ImageButton button, string strMsg)
 322        {
 323            strMsg = strMsg.Replace("/n""file://n/");
 324            strMsg = strMsg.Replace("/r""file://r/");
 325            strMsg = strMsg.Replace("/"""///"");
 326            strMsg = strMsg.Replace("/'""///'");
 327            button.Attributes.Add("onClick""return confirm('" + strMsg + "')");
 328        }

 329
 330        /// <summary>
 331        /// Add the confirm message to one column of gridview
 332        /// 代码调用:
 333        ///         UIHelper myHelp = new UIHelper();
 334        ///         myHelp.AddConfirm(this.GridView1,1, "ok");
 335        /// 请使用时注意,此方法的调用必须实例化,调用
 336        /// </summary>
 337        /// <param name="grid">The control, must be a GridView</param>
 338        /// <param name="intColIndex">The column index. It's usually the column which has the "delete" button.</param>
 339        /// <param name="strMsg">The popup message</param>

 340        public static void AddConfirm(System.Web.UI.WebControls.GridView grid, int intColIndex, string strMsg)
 341        {
 342            strMsg = strMsg.Replace("/n""file://n/");
 343            strMsg = strMsg.Replace("/r""file://r/");
 344            strMsg = strMsg.Replace("/"""///"");
 345            strMsg = strMsg.Replace("/'""///'");
 346            for (int i = 0; i < grid.Rows.Count; i++)
 347            {
 348                grid.Rows[i].Cells[intColIndex].Attributes.Add("onclick""return confirm('" + strMsg + "')");
 349            }

 350        }

 351        #endregion

 352
 353        #region AddShowDialog
 354        /// <summary>
 355        /// Add the javascript method showModalDialog to button
 356        /// 为Button按钮加入一个弹出窗体对话框
 357        /// 代码调用
 358        ///         UIHelper.AddShowDialog(this.Button1, "www.sina.com.cn", 300, 300);
 359        /// 
 360        /// 
 361        /// 
 362        /// </summary>
 363        /// <param name="button">The control, must be a button</param>
 364        /// <param name="strUrl">The page url, including query string</param>
 365        /// <param name="intWidth">Width of window</param>
 366        /// <param name="intHeight">Height of window</param>

 367        public static void AddShowDialog(System.Web.UI.WebControls.Button button, string strUrl, int intWidth, int intHeight)
 368        {
 369            string strScript = "";
 370            strScript += "var strFeatures = 'dialogWidth=" + intWidth.ToString() + "px;dialogHeight=" + intHeight.ToString() + "px;center=yes;help=no;status=no';";
 371            strScript += "var strName ='';";
 372
 373            if (strUrl.Substring(01== "/")
 374            {
 375                strUrl = strUrl.Substring(1, strUrl.Length - 1);
 376            }

 377
 378            strUrl = BaseUrl + "DialogFrame.aspx?URL=" + strUrl;
 379
 380            strScript += "window.showModalDialog(/'" + strUrl + "/',window,strFeatures);return false;";
 381
 382            button.Attributes.Add("onClick", strScript);
 383        }

 384        #endregion

 385
 386        #region  AddShowDialog
 387        /// <summary>
 388        /// Add the javascript method showModalDialog to button
 389        /// </summary>
 390        /// <param name="button">The control, must be a link button</param>
 391        /// <param name="strUrl">The page url, including query string</param>
 392        /// <param name="intWidth">Width of window</param>
 393        /// <param name="intHeight">Height of window</param>

 394        public static void AddShowDialog(System.Web.UI.WebControls.LinkButton button, string strUrl, int intWidth, int intHeight)
 395        {
 396            string strScript = "";
 397            strScript += "var strFeatures = 'dialogWidth=" + intWidth.ToString() + "px;dialogHeight=" + intHeight.ToString() + "px;center=yes;help=no;status=no';";
 398            strScript += "var strName ='';";
 399
 400            if (strUrl.Substring(01== "/")
 401            {
 402                strUrl = strUrl.Substring(1, strUrl.Length - 1);
 403            }

 404
 405            strUrl = BaseUrl + "DialogFrame.aspx?URL=" + strUrl;
 406
 407            strScript += "window.showModalDialog(/'" + strUrl + "/',strName,strFeatures);return false;";
 408
 409            button.Attributes.Add("onClick", strScript);
 410        }

 411        #endregion

 412
 413        #region  AddShowDialog
 414        /// <summary>
 415        /// Add the javascript method showModalDialog to button
 416        /// </summary>
 417        /// <param name="button">The control, must be a button</param>
 418        /// <param name="strUrl">The page url, including query string</param>
 419        /// <param name="intWidth">Width of window</param>
 420        /// <param name="intHeight">Height of window</param>

 421        public static void AddShowDialog(System.Web.UI.WebControls.ImageButton button, string strUrl, int intWidth, int intHeight)
 422        {
 423            string strScript = "";
 424            strScript += "var strFeatures = 'dialogWidth=" + intWidth.ToString() + "px;dialogHeight=" + intHeight.ToString() + "px;center=yes;help=no;status=no';";
 425            strScript += "var strName ='';";
 426
 427            if (strUrl.Substring(01== "/")
 428            {
 429                strUrl = strUrl.Substring(1, strUrl.Length - 1);
 430            }

 431
 432            strUrl = BaseUrl + "DialogFrame.aspx?URL=" + strUrl;
 433
 434            strScript += "window.showModalDialog(/'" + strUrl + "/',window,strFeatures);return false;";
 435
 436            button.Attributes.Add("onClick", strScript);
 437        }

 438        #endregion

 439        #region ClearTextBox
 440        /// <summary>
 441        /// 将选定的TextBox值清空
 442        /// </summary>
 443        /// <param name="myTextBox"></param>

 444        public static void ClearTextBox(System.Web.UI.WebControls.TextBox myTextBox)
 445        {
 446            myTextBox.Attributes.Add("onclick""this.value=''");
 447        }

 448        #endregion

 449
 450        #region OpenWindow
 451        /// <summary>
 452        /// Use "window.open" to popup the window
 453        /// Created : Wang Hui, Feb 24,2006
 454        /// Modified: Wang Hui, Feb 24,2006
 455        /// 打开窗体的对话框功能
 456        /// 代码调用:
 457        /// 
 458        /// 
 459        ///         UIHelper.OpenWindow(this.Page, "www.sina.com.cn", 400, 300);
 460        ///         UIHelper.ShowDialog(this.Page, "lsdjf.com", 300, 200);
 461        /// 
 462        /// 
 463        /// 
 464        /// </summary>
 465        /// <param name="strUrl">The url of window, start with "/", not "http://"</param>
 466        /// <param name="intWidth">Width of popup window</param>
 467        /// <param name="intHeight">Height of popup window</param>

 468        public static void OpenWindow(System.Web.UI.Page pageCurrent, string strUrl, int intWidth, int intHeight, int intLeft, int intTop, string WinName)
 469        {
 470            #region 老版本
 471            //string strScript = "";
 472            //strScript += "var strFeatures = 'width:" + intWidth.ToString() + "px;height:" + intHeight.ToString() + "px';";
 473            //strScript += "var strName ='__WIN';";
 474            ////strScript += "alert(strFeatures);";
 475
 476            ////--- Add by Wang Hui on Feb 27

 477            //if (strUrl.Substring(0, 1) == "/")
 478            //{
 479            //    strUrl = strUrl.Substring(1, strUrl.Length - 1);
 480            //}
 481            ////--- End Add by Wang Hui on Feb 27
 482
 483            //strUrl = BaseUrl + strUrl;
 484
 485            //strScript += "window.open(/"" + strUrl + "/",strName,strFeatures);";
 486
 487            //pageCurrent.RegisterStartupScript(
 488            //    System.Guid.NewGuid().ToString(),
 489            //    "<script language='javascript'>" + strScript + "</script>"
 490            //    );
 491
 492
 493            //pageCurrent.RegisterStartupScript(
 494            //    System.Guid.NewGuid().ToString(),
 495            //    "<script language='javascript'>" + strScript + "</script>"
 496            //    );
 497            #endregion

 498
 499            StringBuilder sb = new StringBuilder();
 500            sb.AppendFormat(@"myleft={0};mytop={1};", intLeft.ToString(), intTop.ToString());
 501            sb.AppendLine();
 502            sb.AppendFormat(@"settings='top=' + mytop + ',left=' + myleft + ',width={0},height={1},location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,fullscreen=no';",
 503                             intWidth.ToString(), intHeight.ToString());
 504            sb.AppendLine();
 505            sb.AppendFormat(@"window.open('{0}','{1}', settings);", strUrl, WinName);
 506
 507            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 508                    System.Guid.NewGuid().ToString(),
 509                    "<script language='javascript'>" + sb.ToString() + "</script>"
 510                    );
 511
 512        }

 513        #endregion

 514
 515        #region ShowDialog
 516        /// <summary>
 517        /// Use "window.showModalDialog" to show the dialog
 518        /// Created : Wang Hui, Feb 24,2006
 519        /// Modified: Wang Hui, Feb 27,2006
 520        /// 此窗体是模式窗体,和C/S结构中的模式窗体是一致的
 521        /// </summary>
 522        /// <param name="strUrl">The url of dialog, start with "/", not "http://"</param>
 523        /// <param name="intWidth">Width of dialog</param>
 524        /// <param name="intHeight">Height of dialog</param>

 525        public static void ShowDialog(System.Web.UI.Page pageCurrent, string strUrl, int intWidth, int intHeight)
 526        {
 527            string strScript = "";
 528            strScript += "var strFeatures = 'dialogWidth=" + intWidth.ToString() + "px;dialogHeight=" + intHeight.ToString() + "px;center=yes;help=no;status=no';";
 529            strScript += "var strName ='';";
 530
 531            //--- Add by Wang Hui on Feb 27 
 532            if (strUrl.Substring(01== "/")
 533            {
 534                strUrl = strUrl.Substring(1, strUrl.Length - 1);
 535            }

 536            //--- End Add by Wang Hui on Feb 27
 537
 538            strUrl = BaseUrl + "DialogFrame.aspx?URL=" + strUrl;
 539
 540            //strScript += "window.showModalDialog(/"" + strUrl + "/",strName,strFeatures);";
 541            strScript += "window.showModalDialog(/"" + strUrl + "/",window,strFeatures); ";
 542
 543            pageCurrent.ClientScript.RegisterStartupScript(
 544                pageCurrent.GetType(), System.Guid.NewGuid().ToString(),
 545                "<script language='javascript'>" + strScript + "</script>"
 546                );
 547        }

 548        #endregion

 549
 550        #region CloseWindows
 551        /// <summary>
 552        /// 关闭窗体,没有任何提示的关闭窗体
 553        /// </summary>
 554        /// <param name="pageCurrent"></param>

 555        public static void CloseWindows(System.Web.UI.Page pageCurrent)
 556        {
 557            StringBuilder sb = new StringBuilder();
 558            sb.Append("<script>window.opener=null;window.close();</script>");
 559            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 560                System.Guid.NewGuid().ToString(), sb.ToString());
 561        }

 562
 563
 564
 565        /// <summary>
 566        /// 有提示信息的关闭窗体
 567        /// </summary>
 568        /// <param name="pageCurrent"></param>
 569        /// <returns></returns>

 570        public static void CloseWindows(System.Web.UI.Page pageCurrent, string strMessage)
 571        {
 572            StringBuilder sb = new StringBuilder();
 573            sb.Append("<script>if(confirm(/"" + strMessage + "/")==true){window.close();}</script>");
 574            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 575                                System.Guid.NewGuid().ToString(), sb.ToString());
 576        }

 577        /// <summary>
 578        /// 有等待时间的关闭窗体
 579        /// </summary>
 580        /// <param name="pageCurrent"></param>
 581        /// <param name="WaitTime">等待时间,以毫秒为记量单位</param>

 582        public static void CloseWindows(System.Web.UI.Page pageCurrent, int WaitTime)
 583        {
 584            StringBuilder sb = new StringBuilder();
 585            sb.Append("<script language=/"javascript/">");
 586            //加入此行功能后没有提提示功能
 587            sb.Append("window.opener=null;");
 588            sb.Append("setTimeout");
 589            sb.Append("(");
 590            sb.Append("'");
 591            sb.Append("window.close()");
 592            sb.Append("'");
 593
 594            sb.Append(",");
 595            sb.Append(WaitTime.ToString());
 596            sb.Append(")");
 597            sb.Append("</script>");
 598            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 599                        System.Guid.NewGuid().ToString(), sb.ToString());
 600
 601        }

 602        #endregion

 603
 604        #region  ShowStatusBar
 605        public static void ShowStatus(System.Web.UI.Page pageCurrent, string StatusString)
 606        {
 607            StringBuilder sb = new StringBuilder();
 608            sb.Append("<script language=/"javascript/">");
 609            sb.Append("window.status=");
 610            sb.Append("/"");
 611            sb.Append(StatusString);
 612            sb.Append("/"");
 613            sb.Append("</script>");
 614            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 615                System.Guid.NewGuid().ToString(), sb.ToString());
 616        }

 617        #endregion

 618
 619        #region  Image水印
 620        /// <summary>
 621        /// 写入图像水印
 622        /// </summary>
 623        /// <param name="str">水印字符串</param>
 624        /// <param name="filePath">原图片位置</param>
 625        /// <param name="savePath">水印加入后的位置</param>
 626        /// <returns></returns>

 627        public static string CreateBackImage(System.Web.UI.Page pageCurrent, string str, string filePath, string savePath, int x, int y)
 628        {
 629            System.Drawing.Image img = System.Drawing.Image.FromFile(pageCurrent.MapPath(filePath));
 630            //创建图片
 631            Graphics graphics = Graphics.FromImage(img);
 632            //指定要绘制的面积
 633            graphics.DrawImage(img, 00, img.Width, img.Height);
 634            //定义字段和画笔
 635            Font font = new Font("黑体"16);
 636            Brush brush = new SolidBrush(Color.Yellow);
 637            graphics.DrawString(str, font, brush, x, y);
 638            //保存并输出图片
 639            img.Save(pageCurrent.MapPath(savePath), System.Drawing.Imaging.ImageFormat.Jpeg);
 640            return savePath;
 641
 642        }

 643        #endregion

 644
 645        #region PlayMediaFile
 646        /// <summary>
 647        /// 调用Media播放mp3或电影文件
 648        /// </summary>
 649        /// <param name="pageCurrent">
 650        /// 当前的页面对象
 651        /// </param>
 652        /// <param name="PlayFilePath">
 653        /// 播放文件的位置
 654        /// </param>
 655        /// <param name="MediajavascriptPath">
 656        /// Mediajavascript的脚本位置
 657        /// </param>
 658        /// <param name="enableContextMenu">
 659        /// 是否可以使用右键
 660        /// 指定是否使右键菜单有效
 661        /// 指定右键是否好用,默认为0不好用
 662        /// 指定为1时就是好用
 663        /// </param>
 664        /// <param name="uiMode">
 665        /// 播放器的大小显示
 666        /// None,mini,或full,指定Windows媒体播放器控制如何显示
 667        /// </param>

 668        public static string PlayMediaFile(System.Web.UI.Page pageCurrent,
 669                        string PlayFilePath, string MediajavascriptPath,
 670                        string enableContextMenu, string uiMode)
 671        {
 672            StreamReader sr = new StreamReader(pageCurrent.MapPath(MediajavascriptPath));
 673            StringBuilder sb = new StringBuilder();
 674            string line;
 675            try
 676            {
 677                while ((line = sr.ReadLine()) != null)
 678                {
 679                    sb.AppendLine(line);
 680
 681                }

 682                sr.Close();
 683            }

 684            catch (Exception ex)
 685            {
 686                throw new Exception(ex.Message);
 687            }

 688            sb.Replace("$URL", pageCurrent.MapPath(PlayFilePath));
 689            sb.Replace("$enableContextMenu", enableContextMenu);
 690            sb.Replace("$uiMode", uiMode);
 691            //pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 692            //            System.Guid.NewGuid().ToString(), sb.ToString());
 693            return sb.ToString();
 694        }

 695        #endregion

 696
 697        #region ShowProgBar
 698        /// <summary>
 699        /// 主要实现进度条的功能,这段代码的调用就要实现进度的调度
 700        /// 实现主要过程
 701        /// default.aspx.cs是调用页面
 702        /// 放入page_load事件中
 703        ///            UIHelper myUI = new UIHelper();
 704        ///            Response.Write(myUI.ShowProgBar(this.Page,"../JS/progressbar.htm"));
 705        ///            Thread thread = new Thread(new ThreadStart(ThreadProc));
 706        ///            thread.Start();
 707        ///            LoadData();//load数据 
 708        ///            thread.Join();
 709        ///            Response.Write("OK");
 710        /// 
 711        /// 其中ThreadProc方法为
 712        ///     public void ThreadProc()
 713        ///    {
 714        ///    string strScript = "<script>setPgb('pgbMain','{0}');</script>";
 715        ///    for (int i = 0; i <= 100; i++)
 716        ///     {
 717        ///        System.Threading.Thread.Sleep(10);
 718        ///        Response.Write(string.Format(strScript, i));
 719        ///        Response.Flush();
 720        ///     }
 721        ///    }
 722        /// 其中LoadData()
 723        ///     public void LoadData()
 724        ///        {
 725        ///            for (int m = 0; m < 900; m++)
 726        ///            {
 727        ///                for (int i = 0; i < 900; i++)
 728        ///                {
 729        ///
 730        ///                }
 731        ///            }
 732        ///        }
 733        /// 
 734        /// </summary>
 735        /// <param name="pageCurrent"></param>
 736        /// <param name="ShowProgbarScript"></param>
 737        /// <returns></returns>

 738        public static string ShowProgBar(System.Web.UI.Page pageCurrent, string ShowProgbarScript)
 739        {
 740            StreamReader sr = new StreamReader(pageCurrent.MapPath(ShowProgbarScript), System.Text.Encoding.Default);
 741            StringBuilder sb = new StringBuilder();
 742            string line;
 743            try
 744            {
 745                while ((line = sr.ReadLine()) != null)
 746                {
 747                    sb.AppendLine(line);
 748
 749                }

 750                sr.Close();
 751            }

 752            catch (Exception ex)
 753            {
 754                throw new Exception(ex.Message);
 755            }

 756            //pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 757            //            System.Guid.NewGuid().ToString(), sb.ToString());
 758            return sb.ToString();
 759        }

 760        #endregion

 761
 762        #region fixedHeader
 763        public static string fixedHeader()
 764        {
 765            StringBuilder s = new StringBuilder();
 766            s.Append(@"<table width='100%' border='1' cellspacing='0' style='MARGIN-TOP:-2px'>");
 767            s.Append(@"<TR class='fixedHeaderTr' style='BACKGROUND:navy;COLOR:white'>");
 768            s.Append(@"<TD nowrap>Header A</TD>");
 769            s.Append(@"<TD nowrap>Header B</TD>");
 770            s.Append(@"<TD nowrap>Header C</TD>");
 771            s.Append(@"</TR>");
 772            for (int m = 0; m < 100; m++)
 773            {
 774                s.Append(@"<TR>");
 775                s.Append(@"<TD>A").Append(m).Append("</TD>");
 776                s.Append(@"<TD>B").Append(m).Append("</TD>");
 777                s.Append(@"<TD>C").Append(m).Append("</TD>");
 778                s.Append(@"</TR>");
 779            }

 780            s.Append(@"</table>");
 781            return s.ToString();
 782        }

 783        #endregion

 784
 785        #region refreshPage
 786        public static void refreshPage(System.Web.UI.Page pageCurrent)
 787        {
 788            StringBuilder sb = new StringBuilder();
 789            sb.Append("<script language=/"javascript/">");
 790            sb.Append("window.location.reload(true);");
 791            sb.Append("</script>");
 792            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
 793                        System.Guid.NewGuid().ToString(), sb.ToString());
 794
 795        }

 796        #endregion

 797
 798        #region Page_revealTrans
 799        //进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">
 800        //推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)"> 
 801        //这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1-23:
 802        //  0 矩形缩小 
 803        //  1 矩形扩大 
 804        //  2 圆形缩小
 805        //  3 圆形扩大 
 806        //  4 下到上刷新 
 807        //  5 上到下刷新
 808        //  6 左到右刷新 
 809        //  7 右到左刷新 
 810        //  8 竖百叶窗
 811        //  9 横百叶窗 
 812        //  10 错位横百叶窗 
 813        //  11 错位竖百叶窗
 814        //  12 点扩散 
 815        //  13 左右到中间刷新 
 816        //  14 中间到左右刷新
 817        //  15 中间到上下
 818        //  16 上下到中间 
 819        //  17 右下到左上
 820        //  18 右上到左下 
 821        //  19 左上到右下 
 822        //  20 左下到右上
 823        //  21 横条 
 824        //  22 竖条 
 825        //  23 以上22种随机选择一种
 826
 827        public static string Page_revealTrans(System.Web.UI.Page currentPage, string duration,
 828                                       string transition)
 829        {
 830            StringBuilder sb = new StringBuilder();
 831            sb.Append("<meta http-equiv=/"Page-Enter/"");
 832            sb.Append("content=/"");
 833            sb.Append("revealTrans(duration=" + duration);
 834            sb.Append(",transition=" + transition);
 835            sb.Append(")/">");
 836            //currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(),
 837            //        System.Guid.NewGuid().ToString(), sb.ToString());
 838            return sb.ToString();
 839        }

 840        #endregion

 841
 842        /**/
 843        /// <summary>
 844        /// 显示一段自定义的输出代码
 845        /// </summary>
 846        /// <param name="page">页面指针,一般为This</param>

 847        public static void ResponseScript(System.Web.UI.Page page, string script)
 848        {
 849            StringBuilder sb = new StringBuilder();
 850            sb.Append("<script language=/"javascript/"> /n");
 851            sb.Append(script.Trim());
 852            sb.Append("</script>");
 853            page.Response.Write(sb.ToString());
 854        }

 855
 856        /// <summary>
 857        /// 调用客户端JavaScript函数
 858        /// </summary>
 859        /// <param name="page">页面指针,一般为This</param>
 860        /// <param name="scriptName">函数名,带参数,如:FunA(1);</param>

 861        public static void CallClientScript(System.Web.UI.Page page, string scriptName)
 862        {
 863            String csname = "PopupScript";
 864            Type cstype = page.GetType();
 865            System.Web.UI.ClientScriptManager cs = page.ClientScript;
 866            if (!cs.IsStartupScriptRegistered(cstype, csname))
 867            {
 868                String cstext = scriptName;
 869                cs.RegisterStartupScript(cstype, csname, cstext, true);
 870            }

 871        }

 872
 873        /// <summary>
 874        /// 弹出对话框(弹出对话框后css会失效)
 875        /// </summary>
 876        /// <param name="message">提示信息</param>

 877        public static void ShowMessage(string message)
 878        {
 879            StringBuilder sb = new StringBuilder();
 880            sb.Append("<script language=/"javascript/"> /n");
 881            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 882            sb.Append("</script>");
 883
 884            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 885        }

 886
 887        /// <summary>
 888        /// 弹出对话框(不影响css样式)
 889        /// </summary>
 890        /// <param name="page">页面指针,一般为this</param>
 891        /// <param name="scriptKey">脚本键,唯一</param>
 892        /// <param name="message">提示信息</param>

 893        public static void ShowMessage(System.Web.UI.Page page, string scriptKey, string message)
 894        {
 895            System.Web.UI.ClientScriptManager csm = page.ClientScript;
 896            if (!csm.IsClientScriptBlockRegistered(scriptKey))
 897            {
 898                string strScript = "alert('" + message + "');";
 899                csm.RegisterClientScriptBlock(page.GetType(), scriptKey, strScript, true);
 900            }

 901        }

 902
 903        /// <summary>
 904        /// 为控件添加确认提示对话框
 905        /// </summary>
 906        /// <param name="Control">需要添加提示的对话框</param>
 907        /// <param name="message">提示信息</param>

 908        public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string message)
 909        {
 910            Control.Attributes.Add("onclick""return confirm('" + message + "');");
 911        }

 912
 913        /**/
 914        /// <summary>
 915        /// 显示一个弹出窗口,并转向目标页(导航)
 916        /// </summary>

 917        public static void ShowAndRedirect(string message, string url)
 918        {
 919            StringBuilder sb = new StringBuilder();
 920            sb.Append("<script language=/"javascript/"> /n");
 921            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 922            sb.Append("window.location.href=/"" + url.Trim().Replace("'", "") + "/";/n");
 923            sb.Append("</script>");
 924
 925            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 926        }

 927
 928        /// <summary>
 929        /// 显示一个弹出窗口,重新加载当前页
 930        /// </summary>

 931        public static void ShowAndReLoad(string message)
 932        {
 933            StringBuilder sb = new StringBuilder();
 934            sb.Append("<script language=/"javascript/"> /n");
 935            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 936            sb.Append("window.location.href=window.location.href;/n");
 937            sb.Append("</script>");
 938
 939            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 940        }

 941
 942        /// <summary>
 943        /// 显示一个弹出窗口,刷新当前页(危险的,有可能陷入死循环)
 944        /// </summary>

 945        public static void ShowAndRefresh(string message)
 946        {
 947            StringBuilder sb = new StringBuilder();
 948            sb.Append("<script language=/"javascript/"> /n");
 949            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 950            sb.Append("document.execCommand('Refresh')");
 951            sb.Append("</script>");
 952
 953            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 954        }

 955
 956        /// <summary>
 957        /// 显示一个弹出窗口,并关闭当前页
 958        /// </summary>
 959        /// <param name="message"></param>

 960        public static void ShowAndClose(string message)
 961        {
 962            System.Text.StringBuilder sb = new System.Text.StringBuilder();
 963            sb.Append("<script language=/"javascript/">/n");
 964            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 965            sb.Append("window.close();/n");
 966            sb.Append("</script>/n");
 967            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 968        }

 969
 970        /// <summary>
 971        /// 显示一个弹出窗口,并转向上一页
 972        /// </summary>
 973        /// <param name="message"></param>

 974        public static void ShowPre(string message)
 975        {
 976            StringBuilder sb = new StringBuilder();
 977            sb.Append("<script language=/"javascript/"> /n");
 978            sb.Append("alert(/"" + message.Trim() + "/"); /n");
 979            sb.Append("var p=document.referrer; /n");
 980            sb.Append("window.location.href=p;/n");
 981            sb.Append("</script>");
 982
 983            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 984        }

 985
 986        /// <summary>
 987        /// 页面重载
 988        /// </summary>

 989        public static void ReLoad()
 990        {
 991            StringBuilder sb = new StringBuilder();
 992            sb.Append("<script language=/"javascript/"> /n");
 993            sb.Append("window.location.href=window.location.href;");
 994            sb.Append("</script>");
 995            System.Web.HttpContext.Current.Response.Write(sb.ToString());
 996
 997        }

 998
 999        /// <summary>
1000        /// 重定向
1001        /// </summary>

1002        public static void Redirect(string url)
1003        {
1004            //string path = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port + url;
1005            string path = "http://" + System.Web.HttpContext.Current.Request.Url.Host + url;
1006            StringBuilder sb = new StringBuilder();
1007            sb.Append("<script language=/"javascript/"> /n");
1008            sb.Append(string.Format("window.location.href='{0}';", @path.Replace("'""")));
1009            sb.Append("</script>");
1010
1011            System.Web.HttpContext.Current.Response.Write(sb.ToString());
1012        }

1013    }

1014}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值