• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET常用方法类-PageBase页面基类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
ASP.NET 常用方法类 - PageBase 页面基类


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

namespace MyBlog.WEB
{
    public class PageBase : System.Web.UI.Page
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="sInputString">需要加密的字符串</param>
        /// <returns>加密后字符串</returns>
        public static string HashString(string sInputString)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sInputString, "MD5");
        }

        /// <summary>
        /// MessageBox提示框
        /// </summary>
        /// <param name="Message">提示信息</param>
        public void MsgBox(string Message)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Message + "');</script>");
            return;
        }

        /// <summary>
        /// MessageBox提示并转向新页面
        /// </summary>
        /// <param name="Message">提示信息</param>
        /// <param name="URL">跳转的路径</param>
        public void MsgBox(string Message, string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Message + "');window.location='" + URL + "';</script>");
            return;
        }
        /// <summary>
        /// 弹出新窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winName">新窗口名称</param>
        /// <param name="winWidth">新窗口宽度</param>
        /// <param name="winHeight">新窗口高度</param>
        public void openNewWin(string pagePath, string winName, int winWidth, int winHeight)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.open('" + pagePath + "','" + winName + "','toolbar=no,menubar=no,titlebar=no,directories=no,resizable=no,scroll=yes,status=no,fullscreen=no,center=yes,width=" + winWidth + ",height=" + winHeight + "');</script>");
            return;
        }

        /// <summary>
        /// 弹出模态窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winWidth">窗口宽度</param>
        /// <param name="winHeight">窗口高度</param>
        public void openModelWin(string pagePath, int winWidth, int winHeight)
        {
            string temp;
            temp = @"<script language='javascript'>";
            temp += (@"   var sPath ='" + pagePath + "';");
            temp += (@"         strFeatures ='dialogWidth=" + winWidth + "px;dialogHeight=" + winHeight + "px;center=yes;scroll=no;help=no;status: No';");
            temp += (@"         var returnvalue = window.showModalDialog(sPath,'brower',strFeatures);");
            temp += (@"</script>");

            ClientScript.RegisterStartupScript(this.GetType(), "message", temp);
        }

        /// <summary>
        /// 弹出控制父窗口的模态窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winWidth">窗口宽度</param>
        /// <param name="winHeight">窗口高度</param>
        /// <param name="frameshref">控制父窗口路径</param>
        public void openModelWin(string pagepath, int width, int height, string frameshref)
        {
            string temp = @"<script language='javascript'>";
            temp += (@"   var sPath ='" + pagepath + "';");
            temp += (@"         strFeatures ='dialogWidth=" + width + "px;dialogHeight=" + height + "px;center=yes;scroll=no;help=no;status=no';");
            temp += (@"         var returnvalue = window.showModalDialog(sPath,'brower',strFeatures);");
            temp += (@"         window.location.href = '" + frameshref + "';");
            temp += (@"</script>");

            ClientScript.RegisterStartupScript(this.GetType(), "message", temp);
        }

        /// <summary>
        /// 关闭本窗口
        /// </summary>
        public void CloseWin()
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.opener=null;window.close();</script>");
            return;
        }

        /// <summary>
        /// 弹出提示信息,并关闭本窗口
        /// <param name="Words">弹出提示信息内容</param>
        /// </summary>
        public void CloseWin(string Words)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('"+Words+"');window.opener=null;window.close();</script>");
            return;
        }


        /// <summary>
        /// 关闭本窗口并,重新加载父窗口.
        /// </summary>
        /// <param name="URL">父窗口要加载的URL</param>
        public void reLoadParentAndClose(string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.opener.location='" + URL + "';window.close();</script>");
            return;
        }

        /// <summary>
        /// 显示提示信息,关闭本窗口并,重新加载父窗口.
        /// </summary>
        /// <param name="Words">提示消息内容</param>
        /// <param name="URL">父窗口要加载的URL</param>
        public void ShowWords_reLoadParentAndClose(string Words,string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Words
+ "'); window.opener.location='" + URL + "';window.close();</script>");
            return;
        }

        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        public string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }


/// <summary>
        /// 获取Session值
        /// </summary>
        /// <param name="SessionID">要获取的Session值</param>
        /// <returns></returns>
        public string GetSession(string SessionID)
        {
            string restr = "unkown";
            try
            {
                restr = Session[SessionID].ToString();
            }
            catch
            {
            }
            if (restr == "unkown" && restr == null && restr == "")
            {
                Response.Redirect("/Error.aspx?Type=NoPopedom");
            }
            return restr;
        }

        /// <summary>
        /// 写Cookie
        /// </summary>
        /// <param name="CookiesName">Cookie集名称</param>
        /// <param name="name">Cookie名</param>
        /// <param name="Value">值</param>
        public void WritCookie(string CookiesName, string name, string Value)
        {
            HttpCookie cookie = new HttpCookie(CookiesName);
            cookie.Values.Add(name, Value);
            Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="CookiesName">Cookie集名称</param>
        /// <param name="name">Cookie名</param>
        /// <param name="Value">值</param>
        public string GetCookie(string CookiesName, string name)
        {
            string value = string.Empty;
            try
            {
                HttpCookie cookie = Request.Cookies[CookiesName];
                value = cookie.Values[name];
            }
            catch
            {
                Response.Redirect("/Error.aspx?Type=GetCookie");
            }
            return value;
        }
    }
}

ASP.NET 常用方法类 - PageBase 页面基类


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

namespace MyBlog.WEB
{
    public class PageBase : System.Web.UI.Page
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="sInputString">需要加密的字符串</param>
        /// <returns>加密后字符串</returns>
        public static string HashString(string sInputString)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sInputString, "MD5");
        }

        /// <summary>
        /// MessageBox提示框
        /// </summary>
        /// <param name="Message">提示信息</param>
        public void MsgBox(string Message)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Message + "');</script>");
            return;
        }

        /// <summary>
        /// MessageBox提示并转向新页面
        /// </summary>
        /// <param name="Message">提示信息</param>
        /// <param name="URL">跳转的路径</param>
        public void MsgBox(string Message, string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Message + "');window.location='" + URL + "';</script>");
            return;
        }
        /// <summary>
        /// 弹出新窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winName">新窗口名称</param>
        /// <param name="winWidth">新窗口宽度</param>
        /// <param name="winHeight">新窗口高度</param>
        public void openNewWin(string pagePath, string winName, int winWidth, int winHeight)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.open('" + pagePath + "','" + winName + "','toolbar=no,menubar=no,titlebar=no,directories=no,resizable=no,scroll=yes,status=no,fullscreen=no,center=yes,width=" + winWidth + ",height=" + winHeight + "');</script>");
            return;
        }

        /// <summary>
        /// 弹出模态窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winWidth">窗口宽度</param>
        /// <param name="winHeight">窗口高度</param>
        public void openModelWin(string pagePath, int winWidth, int winHeight)
        {
            string temp;
            temp = @"<script language='javascript'>";
            temp += (@"   var sPath ='" + pagePath + "';");
            temp += (@"         strFeatures ='dialogWidth=" + winWidth + "px;dialogHeight=" + winHeight + "px;center=yes;scroll=no;help=no;status: No';");
            temp += (@"         var returnvalue = window.showModalDialog(sPath,'brower',strFeatures);");
            temp += (@"</script>");

            ClientScript.RegisterStartupScript(this.GetType(), "message", temp);
        }

        /// <summary>
        /// 弹出控制父窗口的模态窗口
        /// </summary>
        /// <param name="pagePath">页面路径</param>
        /// <param name="winWidth">窗口宽度</param>
        /// <param name="winHeight">窗口高度</param>
        /// <param name="frameshref">控制父窗口路径</param>
        public void openModelWin(string pagepath, int width, int height, string frameshref)
        {
            string temp = @"<script language='javascript'>";
            temp += (@"   var sPath ='" + pagepath + "';");
            temp += (@"         strFeatures ='dialogWidth=" + width + "px;dialogHeight=" + height + "px;center=yes;scroll=no;help=no;status=no';");
            temp += (@"         var returnvalue = window.showModalDialog(sPath,'brower',strFeatures);");
            temp += (@"         window.location.href = '" + frameshref + "';");
            temp += (@"</script>");

            ClientScript.RegisterStartupScript(this.GetType(), "message", temp);
        }

        /// <summary>
        /// 关闭本窗口
        /// </summary>
        public void CloseWin()
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.opener=null;window.close();</script>");
            return;
        }

        /// <summary>
        /// 弹出提示信息,并关闭本窗口
        /// <param name="Words">弹出提示信息内容</param>
        /// </summary>
        public void CloseWin(string Words)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('"+Words+"');window.opener=null;window.close();</script>");
            return;
        }


        /// <summary>
        /// 关闭本窗口并,重新加载父窗口.
        /// </summary>
        /// <param name="URL">父窗口要加载的URL</param>
        public void reLoadParentAndClose(string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>window.opener.location='" + URL + "';window.close();</script>");
            return;
        }

        /// <summary>
        /// 显示提示信息,关闭本窗口并,重新加载父窗口.
        /// </summary>
        /// <param name="Words">提示消息内容</param>
        /// <param name="URL">父窗口要加载的URL</param>
        public void ShowWords_reLoadParentAndClose(string Words,string URL)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + Words
+ "'); window.opener.location='" + URL + "';window.close();</script>");
            return;
        }

        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        public string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }


/// <summary>
        /// 获取Session值
        /// </summary>
        /// <param name="SessionID">要获取的Session值</param>
        /// <returns></returns>
        public string GetSession(string SessionID)
        {
            string restr = "unkown";
            try
            {
                restr = Session[SessionID].ToString();
            }
            catch
            {
            }
            if (restr == "unkown" && restr == null && restr == "")
            {
                Response.Redirect("/Error.aspx?Type=NoPopedom");
            }
            return restr;
        }

        /// <summary>
        /// 写Cookie
        /// </summary>
        /// <param name="CookiesName">Cookie集名称</param>
        /// <param name="name">Cookie名</param>
        /// <param name="Value">值</param>
        public void WritCookie(string CookiesName, string name, string Value)
        {
            HttpCookie cookie = new HttpCookie(CookiesName);
            cookie.Values.Add(name, Value);
            Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="CookiesName">Cookie集名称</param>
        /// <param name="name">Cookie名</param>
        /// <param name="Value">值</param>
        public string GetCookie(string CookiesName, string name)
        {
            string value = string.Empty;
            try
            {
                HttpCookie cookie = Request.Cookies[CookiesName];
                value = cookie.Values[name];
            }
            catch
            {
                Response.Redirect("/Error.aspx?Type=GetCookie");
            }
            return value;
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET中多行textbox限定长度发布时间:2022-07-10
下一篇:
asp.net单点登录设想与构思发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap