http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 6312|回复: 5

[页面辅助] [C#基类库大全]页面辅助类PageHelper

[复制链接]
发表于 2018-12-1 10:02:44 | 显示全部楼层 |阅读模式
[C#基类库大全]页面辅助类PageHelper


导读部分
-------------------------------------------------------------------------------------------------------------
[C#基类库大全]官方产品发布与源码下载---苏飞版
http://www.sufeinet.com/thread-655-1-1.html

这个类有几下几个作用,
  • 锁定页面上的一些控件
  • 解锁页面上的一些空间
  • 禁用控件
  • 开启控件
  • 得到当前页对象实例
  • 读取QueryString值
  • 页面跳转
  • 获取当前请求页面相对于根目录的层级
  • 写javascript脚本
  • 返回客户端浏览器版本


下面我一个一个的给大家介绍一下

1. 锁定页面上的一些控件


[C#] 纯文本查看 复制代码
/// <summary>  
        /// 锁定页面上的一些组件  
        /// </summary>  
        /// <param name="page"></param>  
        /// <param name="obj">不需锁定的控件</param>  
        public static void LockPage(Page page, object[] obj)  
        {  
            Control htmlForm = null;  
            foreach (Control ctl in page.Controls)  
            {  
                if (ctl is HtmlForm)  
                {  
                    htmlForm = ctl;  
                    break;  
                }  
            }  
            //foreach (Control ctl in page.Controls[1].Controls)  
            foreach (Control ctl in htmlForm.Controls)  
            {  
                if (IsContains(obj, ctl) == false)  
                {  
                    //锁定  
                    LockControl(page, ctl);  
                }  
                else  
                {  
                    //解除锁定  
                    UnLockControl(page, ctl);  
                }  
            }  
        }  



2. 解锁页面上的一些空间

[C#] 纯文本查看 复制代码
/// <summary>  
        /// 解除锁定页面上的一些组件  
        /// </summary>  
        /// <param name="page"></param>  
        /// <param name="obj">继续保持锁定的控件</param>  
        public static void UnLockPage(Page page, object[] obj)  
        {  
            Control htmlForm = null;  
            foreach (Control ctl in page.Controls)  
            {  
                if (ctl is HtmlForm)  
                {  
                    htmlForm = ctl;  
                    break;  
                }  
            }  
            //foreach (Control ctl in page.Controls[1].Controls)  
            foreach (Control ctl in htmlForm.Controls)  
            {  
                if (IsContains(obj, ctl) == false)  
                {  
                    //解除锁定  
                    UnLockControl(page, ctl);  
                }  
                else  
                {  
                    //锁定  
                    LockControl(page, ctl);  
                }  
            }  
        }  


3. 禁用控件

[C#] 纯文本查看 复制代码
/// <summary>  
        /// 禁用控件  
        /// </summary>  
        /// <param name="page"></param>  
        /// <param name="ctl"></param>  
        private static void LockControl(Page page, Control ctl)  
        {  
            //WebControl  
            if (ctl is Button || ctl is CheckBox || ctl is HyperLink || ctl is LinkButton  
                || ctl is ListControl || ctl is TextBox)  
            {  
                ((WebControl)ctl).Enabled = false;  
 
                #region 多行文本框不能禁用,应设为只读,不然滚动条不能使用  
  
                if (ctl is TextBox)  
                {  
                    if (((TextBox)ctl).TextMode == TextBoxMode.MultiLine)  
                    {  
                        ((TextBox)ctl).Enabled = true;  
                        ((TextBox)ctl).ReadOnly = true;  
                    }  
                }  
 
                #endregion  
 
                #region 时间控件禁用时不显示图片  
 
 
 
                #endregion  
            }  
  
            //HtmlControl  
            if (ctl is HtmlInputFile)  
            {  
                ((HtmlInputFile)ctl).Disabled = true;  
            }  
        }  


4.开启控件

[C#] 纯文本查看 复制代码
/// <summary>  
        /// 开放控件  
        /// </summary>  
        /// <param name="page"></param>  
        /// <param name="ctl"></param>  
        private static void UnLockControl(Page page, Control ctl)  
        {  
            //WebControl  
            if (ctl is Button || ctl is CheckBox || ctl is HyperLink || ctl is LinkButton  
                || ctl is ListControl || ctl is TextBox)  
            {  
                ((WebControl)ctl).Enabled = true;  
  
                //文本框去掉只读属性  
                if (ctl is TextBox)  
                {  
                    ((TextBox)ctl).ReadOnly = false;  
                }  
  
                ////时间输入文本框不禁用时显示按钮  
                //if (ctl is WebDateTimeEdit)  
                //{  
                //    ((WebDateTimeEdit)ctl).SpinButtons.Display = ButtonDisplay.OnRight;  
                //}  
  
                ////时间选择文本框不禁用时显示按钮  
                //if (ctl is WebDateChooser)  
                //{  
                //    page.ClientScript.RegisterStartupScript(typeof(string), "Display" + ctl.ClientID + "Image", "<script language=javascript>" +  
                //        "document.getElementById('" + ctl.ClientID + "_img" + "').style.display='';</script>");  
                //}  
            }  
  
            //HtmlControl  
            if (ctl is HtmlInputFile)  
            {  
                ((HtmlInputFile)ctl).Disabled = false;  
            }  
        }  


5.得到当前页对象实例

[C#] 纯文本查看 复制代码
/// <summary>  
        /// 得到当前页对象实例  
        /// </summary>  
        /// <returns></returns>  
        public static Page GetCurrentPage()  
        {  
            return (Page)HttpContext.Current.Handler;  
        }  
  
        /// <summary>  
        /// 从System.Web.HttpRequest的Url中获取所调用的页面名称  
        /// </summary>  
        /// <returns>页面名称</returns>  
        public static string GetPageName()  
        {  
            int start = 0;  
            int end = 0;  
            string Url = HttpContext.Current.Request.RawUrl;  
            start = Url.LastIndexOf("/") + 1;  
            end = Url.IndexOf("?");  
            if (end <= 0)  
            {  
                return Url.Substring(start, Url.Length - start);  
            }  
            else  
            {  
                return Url.Substring(start, end - start);  
            }  
        }  


6.读取QueryString值
[C#] 纯文本查看 复制代码
        /// <summary>  
        /// 读取QueryString值  
        /// </summary>  
        /// <param name="queryStringName">QueryString名称</param>  
        /// <returns>QueryString值</returns>  
        public static string GetQueryString(string queryStringName)  
        {  
            if ((HttpContext.Current.Request.QueryString[queryStringName] != null) &&  
                (HttpContext.Current.Request.QueryString[queryStringName] != "undefined"))  
            {  
                return HttpContext.Current.Request.QueryString[queryStringName].Trim();  
            }  
            else  
            {  
                return "";  
            }  
        } 


7.页面跳转

[C#] 纯文本查看 复制代码
        /// <summary>  
        /// 页面跳转  
        /// </summary>  
        /// <param name="url">URL地址</param>  
        public void Redirect(string url)  
        {  
            Page page = GetCurrentPage();  
            page.Response.Redirect(url);  
        }  
  
        /// <summary>  
        /// 获取当前请求页面相对于根目录的层级  
        /// </summary>  
        /// <returns></returns>  
        public static string GetRelativeLevel()  
        {  
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath;  
            if (ApplicationPath.Trim() == "/")  
            {  
                ApplicationPath = "";  
            }  
  
            int i = ApplicationPath == "" ? 1 : 2;  
            return "";//Nandasoft.Helper.NDHelperString.Repeat("../", Nandasoft.Helper.NDHelperString.RepeatTime(HttpContext.Current.Request.Path, "/") - i);  
        }  



8.获取当前请求页面相对于根目录的层级


[C#] 纯文本查看 复制代码
 /// <summary> 
/// 获取当前请求页面相对于根目录的层级 
/// </summary> 
/// <returns></returns> 
public static string GetRelativeLevel() 
{ 
string ApplicationPath = HttpContext.Current.Request.ApplicationPath; 
if (ApplicationPath.Trim() == "/") 
{ 
ApplicationPath = ""; 
} 

int i = ApplicationPath == "" ? 1 : 2; 
return "";//Nandasoft.Helper.NDHelperString.Repeat("../", Nandasoft.Helper.NDHelperString.RepeatTime(HttpContext.Current.Request.Path, "/") - i); 
} 

9.写javascript脚本

[C#] 纯文本查看 复制代码
 /// <summary> 
/// 写javascript脚本 
/// </summary> 
/// <param name="script">脚本内容</param> 
public static void WriteScript(string script) 
{ 
Page page = GetCurrentPage(); 

NDGridViewScriptFirst(page.Form.Controls, page); 

ScriptManager.RegisterStartupScript(page, page.GetType(), System.Guid.NewGuid().ToString(), script, true); 

} 

private void NDGridViewScriptFirst(ControlCollection ctls, Page page) 
{ 

 foreach (Control ctl in ctls) 
{ 
 if (ctl is NDGridView) 
 { 
 NDGridView ndgv = (NDGridView)ctl; 
 ScriptManager.RegisterStartupScript(page, page.GetType(), ndgv.ClientScriptKey, ndgv.ClientScriptName, true); 
 } 
 else 
 { 
 NDGridViewScriptFirst(ctl.Controls, page); 
 } 
} 
} 


10.返回客户端浏览器版本


[C#] 纯文本查看 复制代码
        /// <summary>  
        /// 返回客户端浏览器版本  
        /// 如果是IE类型,返回版本数字  
        /// 如果不是IE类型,返回-1  
        /// </summary>  
        /// <returns>一位数字版本号</returns>  
        public static int GetClientBrowserVersion()  
        {  
            string USER_AGENT = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];  
  
            if (USER_AGENT.IndexOf("MSIE") < 0) return -1;  
  
            string version = USER_AGENT.Substring(USER_AGENT.IndexOf("MSIE") + 5, 1);  
            if (!Utility.IsInt(version)) return -1;  
  
            return Convert.ToInt32(version);  
        } 



下面是全部代码
[C#] 纯文本查看 复制代码
 /// <summary>
/// 类说明:Assistant
/// 编 码 人:苏飞
/// 联系方式:361983679  
/// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http://www.sufeinet.com/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DotNet.Utilities
{
    public class PageHelper
    {
        #region 控件状态设置

        /// <summary>
        /// 锁定页面上的一些组件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="obj">不需锁定的控件</param>
        public static void LockPage(Page page, object[] obj)
        {
            Control htmlForm = null;
            foreach (Control ctl in page.Controls)
            {
                if (ctl is HtmlForm)
                {
                    htmlForm = ctl;
                    break;
                }
            }
            //foreach (Control ctl in page.Controls[1].Controls)
            foreach (Control ctl in htmlForm.Controls)
            {
                if (IsContains(obj, ctl) == false)
                {
                    //锁定
                    LockControl(page, ctl);
                }
                else
                {
                    //解除锁定
                    UnLockControl(page, ctl);
                }
            }
        }

        /// <summary>
        /// 解除锁定页面上的一些组件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="obj">继续保持锁定的控件</param>
        public static void UnLockPage(Page page, object[] obj)
        {
            Control htmlForm = null;
            foreach (Control ctl in page.Controls)
            {
                if (ctl is HtmlForm)
                {
                    htmlForm = ctl;
                    break;
                }
            }
            //foreach (Control ctl in page.Controls[1].Controls)
            foreach (Control ctl in htmlForm.Controls)
            {
                if (IsContains(obj, ctl) == false)
                {
                    //解除锁定
                    UnLockControl(page, ctl);
                }
                else
                {
                    //锁定
                    LockControl(page, ctl);
                }
            }
        }

        /// <summary>
        /// 禁用控件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="ctl"></param>
        private static void LockControl(Page page, Control ctl)
        {
            //WebControl
            if (ctl is Button || ctl is CheckBox || ctl is HyperLink || ctl is LinkButton
                || ctl is ListControl || ctl is TextBox)
            {
                ((WebControl)ctl).Enabled = false;

                #region 多行文本框不能禁用,应设为只读,不然滚动条不能使用

                if (ctl is TextBox)
                {
                    if (((TextBox)ctl).TextMode == TextBoxMode.MultiLine)
                    {
                        ((TextBox)ctl).Enabled = true;
                        ((TextBox)ctl).ReadOnly = true;
                    }
                }

                #endregion

                #region 时间控件禁用时不显示图片



                #endregion
            }

            //HtmlControl
            if (ctl is HtmlInputFile)
            {
                ((HtmlInputFile)ctl).Disabled = true;
            }
        }

        /// <summary>
        /// 开放控件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="ctl"></param>
        private static void UnLockControl(Page page, Control ctl)
        {
            //WebControl
            if (ctl is Button || ctl is CheckBox || ctl is HyperLink || ctl is LinkButton
                || ctl is ListControl || ctl is TextBox)
            {
                ((WebControl)ctl).Enabled = true;

                //文本框去掉只读属性
                if (ctl is TextBox)
                {
                    ((TextBox)ctl).ReadOnly = false;
                }

                ////时间输入文本框不禁用时显示按钮
                //if (ctl is WebDateTimeEdit)
                //{
                //    ((WebDateTimeEdit)ctl).SpinButtons.Display = ButtonDisplay.OnRight;
                //}

                ////时间选择文本框不禁用时显示按钮
                //if (ctl is WebDateChooser)
                //{
                //    page.ClientScript.RegisterStartupScript(typeof(string), "Display" + ctl.ClientID + "Image", "<script language=javascript>" +
                //        "document.getElementById('" + ctl.ClientID + "_img" + "').style.display='';</script>");
                //}
            }

            //HtmlControl
            if (ctl is HtmlInputFile)
            {
                ((HtmlInputFile)ctl).Disabled = false;
            }
        }

        /// <summary>
        /// 数组中是否包含当前控件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ctl"></param>
        /// <returns></returns>
        private static bool IsContains(object[] obj, Control ctl)
        {
            foreach (Control c in obj)
            {
                if (c.ID == ctl.ID)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region 页面处理其它辅助方法

        /// <summary>
        /// 得到当前页对象实例
        /// </summary>
        /// <returns></returns>
        public static Page GetCurrentPage()
        {
            return (Page)HttpContext.Current.Handler;
        }

        /// <summary>
        /// 从System.Web.HttpRequest的Url中获取所调用的页面名称
        /// </summary>
        /// <returns>页面名称</returns>
        public static string GetPageName()
        {
            int start = 0;
            int end = 0;
            string Url = HttpContext.Current.Request.RawUrl;
            start = Url.LastIndexOf("/") + 1;
            end = Url.IndexOf("?");
            if (end <= 0)
            {
                return Url.Substring(start, Url.Length - start);
            }
            else
            {
                return Url.Substring(start, end - start);
            }
        }

        /// <summary>
        /// 读取QueryString值
        /// </summary>
        /// <param name="queryStringName">QueryString名称</param>
        /// <returns>QueryString值</returns>
        public static string GetQueryString(string queryStringName)
        {
            if ((HttpContext.Current.Request.QueryString[queryStringName] != null) &&
                (HttpContext.Current.Request.QueryString[queryStringName] != "undefined"))
            {
                return HttpContext.Current.Request.QueryString[queryStringName].Trim();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="url">URL地址</param>
        public void Redirect(string url)
        {
            Page page = GetCurrentPage();
            page.Response.Redirect(url);
        }

        /// <summary>
        /// 获取当前请求页面相对于根目录的层级
        /// </summary>
        /// <returns></returns>
        public static string GetRelativeLevel()
        {
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath;
            if (ApplicationPath.Trim() == "/")
            {
                ApplicationPath = "";
            }

            int i = ApplicationPath == "" ? 1 : 2;
            return "";//Nandasoft.Helper.NDHelperString.Repeat("../", Nandasoft.Helper.NDHelperString.RepeatTime(HttpContext.Current.Request.Path, "/") - i);
        }

        /// <summary>
        /// 写javascript脚本
        /// </summary>
        /// <param name="script">脚本内容</param>
        public static void WriteScript(string script)
        {
            Page page = GetCurrentPage();

            // NDGridViewScriptFirst(page.Form.Controls, page);

            //ScriptManager.RegisterStartupScript(page, page.GetType(), System.Guid.NewGuid().ToString(), script, true);

        }

        //private void NDGridViewScriptFirst(ControlCollection ctls, Page page)
        //{

        //    foreach (Control ctl in ctls)
        //    {
        //        if (ctl is NDGridView)
        //        {
        //            NDGridView ndgv = (NDGridView)ctl;
        //            ScriptManager.RegisterStartupScript(page, page.GetType(), ndgv.ClientScriptKey, ndgv.ClientScriptName, true);
        //        }
        //        else
        //        {
        //            NDGridViewScriptFirst(ctl.Controls, page);
        //        }
        //    }
        //}

        /// <summary>
        /// 返回客户端浏览器版本
        /// 如果是IE类型,返回版本数字
        /// 如果不是IE类型,返回-1
        /// </summary>
        /// <returns>一位数字版本号</returns>
        public static int GetClientBrowserVersion()
        {
            string USER_AGENT = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

            if (USER_AGENT.IndexOf("MSIE") < 0) return -1;

            string version = USER_AGENT.Substring(USER_AGENT.IndexOf("MSIE") + 5, 1);
            if (!Utility.IsInt(version)) return -1;

            return Convert.ToInt32(version);
        }

        #endregion
    }
}

也可直接下载附件
PageHelper.zip (160 Bytes, 下载次数: 15)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2018-12-1 10:03:28 | 显示全部楼层
发表于 2018-12-1 10:16:34 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-1 10:20:41 | 显示全部楼层
真是难得给力的帖子啊。
发表于 2018-12-1 10:21:55 | 显示全部楼层
楼主的帖子不错, 学习下,收藏
发表于 2018-12-1 10:28:39 | 显示全部楼层
真是难得给力的帖子啊。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-23 21:18

© 2014-2021

快速回复 返回顶部 返回列表