using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Ant.Service.Utilities
{
public class PageHelper
{
#region 控件状态设置
///
/// 锁定页面上的一些组件
///
///
/// 不需锁定的控件
public static void LockPage(Page page, object[] obj)
{
System.Web.UI.Control htmlForm = null;
foreach (System.Web.UI.Control ctl in page.Controls)
{
if (ctl is HtmlForm)
{
htmlForm = ctl;
break;
}
}
//foreach (Control ctl in page.Controls[1].Controls)
foreach (System.Web.UI.Control ctl in htmlForm.Controls)
{
if (IsContains(obj, ctl) == false)
{
//锁定
LockControl(page, ctl);
}
else
{
//解除锁定
UnLockControl(page, ctl);
}
}
}
///
/// 解除锁定页面上的一些组件
///
///
/// 继续保持锁定的控件
public static void UnLockPage(Page page, object[] obj)
{
System.Web.UI.Control htmlForm = null;
foreach (System.Web.UI.Control ctl in page.Controls)
{
if (ctl is HtmlForm)
{
htmlForm = ctl;
break;
}
}
//foreach (Control ctl in page.Controls[1].Controls)
foreach (System.Web.UI.Control ctl in htmlForm.Controls)
{
if (IsContains(obj, ctl) == false)
{
//解除锁定
UnLockControl(page, ctl);
}
else
{
//锁定
LockControl(page, ctl);
}
}
}
///
/// 禁用控件
///
///
///
private static void LockControl(Page page, System.Web.UI.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;
}
}
///
/// 开放控件
///
///
///
private static void UnLockControl(Page page, System.Web.UI.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", "");
//}
}
//HtmlControl
if (ctl is HtmlInputFile)
{
((HtmlInputFile)ctl).Disabled = false;
}
}
///
/// 数组中是否包含当前控件
///
///
///
///
private static bool IsContains(object[] obj, System.Web.UI.Control ctl)
{
foreach (System.Web.UI.Control c in obj)
{
if (c.ID == ctl.ID)
{
return true;
}
}
return false;
}
#endregion
#region 页面处理其它辅助方法
///
/// 得到当前页对象实例
///
///
public static Page GetCurrentPage()
{
return (Page)HttpContext.Current.Handler;
}
///
/// 从System.Web.HttpRequest的Url中获取所调用的页面名称
///
/// 页面名称
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);
}
}
///
/// 读取QueryString值
///
/// QueryString名称
/// QueryString值
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 "";
}
}
///
/// 页面跳转
///
/// URL地址
public void Redirect(string url)
{
Page page = GetCurrentPage();
page.Response.Redirect(url);
}
///
/// 获取当前请求页面相对于根目录的层级
///
///
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);
}
///
/// 写javascript脚本
///
/// 脚本内容
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);
// }
// }
//}
///
/// 返回客户端浏览器版本
/// 如果是IE类型,返回版本数字
/// 如果不是IE类型,返回-1
///
/// 一位数字版本号
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
}
}