常用方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CommHelper
{
public class CWeb
{
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
/// <param name="message">窗口信息</param>
public static void Message(string message)
{
StringBuilder str = new StringBuilder();
str.Append("<script language=\"javascript\" type=\"text/javascript\">");
str.Append("alert('");
str.Append(message);
str.Append("');</script>");
HttpContext.Current.Response.Write(str);
}
/// <summary>
/// 处理JavaScript
/// </summary>
/// <param name="page"></param>
/// <param name="script"></param>
public static void ResponseScript(Page page, string script)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language=\"javascript\" type=\"text/javascript\" defer>" + script + "</script>");
}
/// <summary>
/// 提示小窗口
/// </summary>
/// <param name="page"></param>
/// <param name="msg"></param>
public static void Show(Page page, string msg)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language=\"javascript\" type=\"text/javascript\" defer>alert('" + msg.ToString() + "');</script>");
}
/// <summary>
/// 提示并转至新页面
/// </summary>
/// <param name="page"></param>
/// <param name="msg"></param>
/// <param name="url"></param>
public static void ShowAndRedirects(Page page, string msg, string url)
{
StringBuilder builder = new StringBuilder();
builder.Append("<script language=\"javascript\" type=\"text/javascript\" defer>");
builder.AppendFormat("alert('{0}');", msg);
builder.AppendFormat("top.location.href='{0}'", url);
builder.Append("</script>");
page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
}
/// <summary>
/// 确认对话框
/// </summary>
/// <param name="Control"></param>
/// <param name="msg"></param>
public static void ShowConfirm(WebControl Control, string msg)
{
Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
}
/// <summary>
/// 打开窗口大小,名称,屏蔽ie导航,新窗口居中
/// </summary>
/// <param name="url"></param>
/// <param name="formName"></param>
/// <param name="height"></param>
/// <param name="width"></param>
public static void openDialog(string strURL, double intWidth, double intHeight, string aryParam, int style)
{
if (style == 1 || style == 3 || style == 5 || style == 7)
{
intHeight *= 1.05;
intWidth *= 1.1;
}
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
sb.Append("var h = window.screen.availHeight;");
sb.Append("var w = window.screen.availWidth;");
sb.Append("var objh = " + intHeight + ";");
sb.Append("var objw = " + intWidth + ";");
sb.Append("var Settop = (h-objh)/ 2;");
sb.Append("var Setleft = (w-objw)/ 2;");
sb.Append("window.open('");
sb.Append(strURL);
sb.Append("','");
sb.Append(aryParam);
sb.Append("',");
sb.Append("'height=");
sb.Append(intHeight);
sb.Append(",width=");
sb.Append(intWidth);
sb.Append(",top='+Settop+'");
sb.Append(",left='+Setleft+'");
sb.Append(",status=no,resizable=no,scrollbars=yes');");
sb.Append("</script>");
HttpContext.Current.Response.Write(sb);
}
/// <summary>
/// Open方式打开
/// </summary>
/// <param name="url"></param>
/// <param name="isFullScreen"></param>
public static void OpenWebForm(string url, bool isFullScreen)
{
string js = "<script language=\"javascript\" type=\"text/javascript\">";
if (isFullScreen)
{
js += "var iWidth = 0;";
js += "var iHeight = 0;";
js += "iWidth=window.screen.availWidth-10;";
js += "iHeight=window.screen.availHeight-50;";
js += "var szFeatures ='width=' + iWidth + ',height=' + iHeight + ',top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no';";
js += "window.open('" + url + @"','',szFeatures);";
}
else
{
js += "window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');";
}
js += "</script>";
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 返回上一页
/// </summary>
public static void BackBefore()
{
StringBuilder str = new StringBuilder();
str.Append("<script language=\"JavaScript\" type=\"text/javascript\">");
str.Append("window.history.go(-1);");
str.Append("</script>");
HttpContext.Current.Response.Write(str);
}
/// <summary>
/// 应用程序路径
/// </summary>
public static string ApplicationPath
{
get{
string path = HttpRuntime.AppDomainAppVirtualPath;
if (path == "/")
return "";
return path;
}
}
/// <summary>
/// 获取表单或Url中的值
/// </summary>
/// <param name="asName"></param>
/// <returns></returns>
public static string GetRequestValue(string asName)
{
string sRet = "";
if (HttpContext.Current.Request.Form.Get(asName) != null && HttpContext.Current.Request.Form.Get(asName).Trim() != "")
{
return HttpContext.Current.Request.Form.Get(asName);
}
if (HttpContext.Current.Request.QueryString.Get(asName) != null && HttpContext.Current.Request.QueryString.Get(asName).Trim() != "")
{
return HttpContext.Current.Request.QueryString.Get(asName);
}
return sRet;
}
}
}