• .Net中常用的JS脚本方法

    日期:2009-07-01 | 分类:.net

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://alek.blogbus.com/logs/41738787.html

    .Net中常用的JS脚本方法

            /// 删除记录确认
            /// </summary>
            /// <param name="button">需要确认的按钮</param>
            public static void Confirm(WebControl button)
            {
                button.Attributes.Add("onclick", "return confirm(\"确定要删除此项记录吗?\")");
            }

            /// <summary>
            /// 点击按钮确认
            /// </summary>
            /// <param name="button">需要确认的按钮</param>
            /// <param name="Message">提示信息</param>
            public static void Confirm(WebControl button, string Message)
            {
                button.Attributes.Add("onclick", "return confirm('" + Message + "')");
            }

     


            /// <summary>
            /// 弹出对话框
            /// </summary>
            /// <param name="msg">提示信息</param>
            public static void Alert(string msg)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg.Trim() + "')<" + "/" + "script>");
            }

            /// <summary>
            /// 关闭当前窗口
            /// </summary>
            public static void CloseWin()
            {
                HttpContext.Current.Response.Write("<script language='javascript'>window.close();<" + "/" + "script>");

            }

            /// <summary>
            /// 当前窗口是,由一个窗口弹出的子窗口,本方法可以实现父窗口重定向
            /// </summary>
            /// <param name="page">跳转到的页面地址</param>
            public static void PageRedirect(string page)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>window.opener.location='" + page + "';<" + "/" + "script>");
            }

            /// <summary>
            /// 本窗口重定向
            /// </summary>
            /// <param name="page">跳转到的页面地址</param>
            public static void Redirect(string page)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>location='" + page + "';<" + "/" + "script>");
            }

            /// <summary>
            /// 当前页面刷新
            /// </summary>
            public static void PageReload()
            {
                HttpContext.Current.Response.Write("<script>window.opener.location.reload();</script>");
            }

            /// <summary>
            /// 弹出窗口
            /// </summary>
            /// <param name="url">页面路径</param>
            public static void OpenWindow(string url)
            {
                string script = "<script language='javascript'>window.open('" + url + "', 'newwindow', 'height=640, width=600, top=10,left=180,toolbar=no, menubar=no,scrollbars=yes,  resizable=no, location=no,status=no')<" + "/" + "script>";
                HttpContext.Current.Response.Write(script);

            }

            /// <summary>
            /// 弹出窗口
            /// </summary>
            /// <param name="url">页面路径</param>
            /// <param name="Height">页面高度</param>
            /// <param name="Width">页面宽度</param>
            public static void OpenWindow(string url, int Height, int Width)
            {
                string script = "<script language='javascript'>window.open('" + url + "', 'newwindow', 'height=" + Height + ", width=" + Width + ", top=10,left=180,toolbar=no, menubar=no,scrollbars=yes,  resizable=no, location=no,status=no')<" + "/" + "script>";
                HttpContext.Current.Response.Write(script);

            }

            /// <summary>
            /// 弹出窗口
            /// </summary>
            /// <param name="url">页面路径</param>
            /// <param name="Height">页面高度</param>
            /// <param name="Width">页面宽度</param>
            /// <param name="top">页面顶不边距</param>
            /// <param name="left">页面左布边距</param>
            public static void OpenWindow(string url, int Height, int Width, int top, int left)
            {
                string script = "<script language='javascript'>window.open('" + url + "', 'newwindow', 'height=" + Height + ", width=" + Width + ", top=" + top + ",left=" + left + ",toolbar=no, menubar=no,scrollbars=yes,  resizable=no, location=no,status=no')<" + "/" + "script>";
                HttpContext.Current.Response.Write(script);

            }


            /// <summary>
            /// 打印当前页
            /// </summary>
            public static void Print()
            {
                HttpContext.Current.Response.Write("<script language=javascript>window.print();<" + "/" + "script>");
            }

            //  /// <summary>
            //  /// 点击后弹出新页面
            //  /// </summary>
            //  /// <param name="e"></param>
            //  public static void Re(DataGridItemEventArgs e)
            //  {
            //   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //       e.Item.Attributes.Add("onclick","window.open('Default.aspx?id=" + e.Item.Cells[0].Text + "');");
            //
            //  }
            //
            //  双击表格连接到另一页
            //  
            //    在itemDataBind事件中
            //
            //  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //  {
            //    string OrderItemID =e.item.cells[1].Text;
            //    ...
            //    e.item.Attributes.Add("ondblclick", "location.href=’../ShippedGrid.aspx?id=" + OrderItemID + "’");
            //  }
            //  
            //  双击表格打开新一页
            //   
            //  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //  {
            //     string OrderItemID =e.item.cells[1].Text;
            //     ...
            //     e.item.Attributes.Add("ondblclick", "open(’../ShippedGrid.aspx?id=" + OrderItemID + "’)");
            //  }

            //  8.关于日期格式
            //  
            //     日期格式设定
            //  
            //     DataFormatString="{0:yyyy-MM-dd}"
            //  
            //    我觉得应该在itembound事件中
            //  
            //  e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))
            //  
            //  9.获取错误信息并到指定页面
            //  
            //    不要使用Response.Redirect,而应该使用Server.Transfer
            //  
            //    e.g
            //  
            //    // in global.asax
            //    protected void Application_Error(Object sender, EventArgs e)
            //    {
            //     if (Server.GetLastError() is HttpUnhandledException)
            //      Server.Transfer("MyErrorPage.aspx");
            //  
            //     //其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
            //    }
            //  
            //    Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理

     

            /// <summary>
            /// 表格点击改变颜色
            /// </summary>
            /// <param name="e">DataGridItemEventArgs DataGrid1_ItemDataBound事件参数</param>
            public static void OnMouseClick(DataGridItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    e.Item.Attributes.Add("onclick", "if(this.style.backgroundColor=='#ff6600')this.style.backgroundColor='#ffffff';else this.style.backgroundColor='#ff6600'; this.style.color='buttontext';this.style.cursor='default';");
                }

            }

            /// <summary>
            /// 表格点击改变颜色
            /// </summary>
            /// <param name="e">DataGridItemEventArgs DataGrid1_ItemDataBound事件参数</param>
            /// <param name="ChangeColor">点击后的颜色</param>
            /// <param name="DefaultColor">取消还原的本色</param>
            public static void OnMouseClick(DataGridItemEventArgs e, string ChangeColor, string DefaultColor)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    e.Item.Attributes.Add("onclick", "if(this.style.backgroundColor=='" + ChangeColor + "')this.style.backgroundColor='" + DefaultColor + "';else this.style.backgroundColor='" + ChangeColor + "'; this.style.color='buttontext';this.style.cursor='default';");
                }

            }

            /// <summary>
            /// 添加数据网格显示效果
            /// </summary>
            /// <param name="e">DataGridItemEventArgs DataGrid1_ItemDataBound事件参数</param>
            public static void OnMouseOver(DataGridItemEventArgs e)
            {
                ListItemType itemType = e.Item.ItemType;
                if (itemType == ListItemType.Item)
                {

                    e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
                    e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";

                }
                else if (itemType == ListItemType.AlternatingItem)
                {
                    e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
                    e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
                }
            }

            /// <summary>
            /// 添加数据网格显示效果
            /// </summary>
            /// <param name="e">DataGridItemEventArgs DataGrid1_ItemDataBound事件参数</param>
            /// <param name="MouseOverColor"></param>
            public static void OnMouseOver(DataGridItemEventArgs e, string MouseOverColor)
            {
                ListItemType itemType = e.Item.ItemType;
                if (itemType == ListItemType.Item)
                {

                    e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
                    e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='" + MouseOverColor + "';cursor='hand';";

                }
                else if (itemType == ListItemType.AlternatingItem)
                {
                    e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
                    e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='" + MouseOverColor + "';cursor='hand';";
                }
            }
    http://mwga.net.blog.163.com/blog/static/10645672820095202584261/


    收藏到:Del.icio.us