You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.9 KiB
C#

11 months ago
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI;
namespace WebSqlHelper
{
public class HttpHelper
{
// Fields
private static string applicationpath = HttpContext.Current.Request.ApplicationPath;
public static string ApplicationPath = ((((applicationpath == null) || (applicationpath == "")) || (applicationpath == "/")) ? "/" : (applicationpath + "/").Replace("//", "/"));
// Methods
public static void BindDropDownList(CheckBoxList drop, object datasource, string text, string value)
{
drop.DataSource = datasource;
drop.DataTextField = text;
drop.DataValueField = value;
drop.DataBind();
}
public static void BindDropDownList(DropDownList drop, object datasource, string text, string value)
{
BindDropDownList(drop, datasource, text, value, 0);
}
public static void BindDropDownList(RadioButtonList drop, object datasource, string text, string value)
{
drop.DataSource = datasource;
drop.DataTextField = text;
drop.DataValueField = value;
drop.DataBind();
}
public static void BindDropDownList(DropDownList drop, object datasource, string text, string value, int flag)
{
drop.DataSource = datasource;
drop.DataTextField = text;
drop.DataValueField = value;
drop.DataBind();
if (flag == 1)
{
drop.Items.Insert(0, new ListItem("---请选择---", ""));
}
}
public static void BindDropDownList(DropDownList drop, object datasource, string text, string value, string flag)
{
drop.DataSource = datasource;
drop.DataTextField = text;
drop.DataValueField = value;
drop.DataBind();
if ((flag != null) && (flag != ""))
{
drop.Items.Insert(0, new ListItem(flag, ""));
}
}
public static void BindDropDownList(RadioButtonList drop, object datasource, string text, string value, string flag)
{
drop.DataSource = datasource;
drop.DataTextField = text;
drop.DataValueField = value;
drop.DataBind();
if ((flag != null) && (flag != ""))
{
drop.Items.Add(new ListItem(flag, ""));
}
}
public static void BindGrid(DataList rep, object datasource)
{
rep.DataSource = datasource;
rep.DataBind();
}
public static void BindGrid(GridView rep, object datasource)
{
rep.DataSource = datasource;
rep.DataBind();
}
public static void BindGrid(Repeater rep, object datasource)
{
rep.DataSource = datasource;
rep.DataBind();
}
public static string BindUrl(string s)
{
if (s.IndexOf("?") != -1)
{
s = s + "&";
return s;
}
s = s + "?";
return s;
}
public static string BindUrl(string s, string id)
{
if (s.IndexOf("moduleid=") == -1)
{
if (s.IndexOf("?") != -1)
{
s = s + "&";
}
else
{
s = s + "?";
}
s = s + "moduleid=" + id;
}
return s;
}
public static string ComBindUrl(string s)
{
if (s.IndexOf("?") != -1)
{
s = s + "&";
return s;
}
s = s + "?";
return s;
}
public static string GetModuleFromU(string qparam)
{
string str = null;
if (((qparam == null) || !(qparam != "")) || (qparam.ToLower().IndexOf("moduleid") == -1))
{
return str;
}
str = qparam.Substring(qparam.ToLower().IndexOf("moduleid"));
if ((str.IndexOf("&") != -1) && (str.IndexOf("=") != -1))
{
int index = str.IndexOf("&");
int num2 = str.IndexOf("=");
string oldValue = str.Substring(str.IndexOf("&"));
return str.TrimStart("moduleid=".ToCharArray()).Replace(oldValue, "");
}
return str.TrimStart("moduleid=".ToCharArray());
}
public static string GetPic(string s)
{
try
{
if (File.Exists(HttpContext.Current.Server.MapPath(ApplicationPath + s)))
{
return (ApplicationPath + s).Replace("//", "/");
}
return "";
}
catch
{
return "";
}
}
public static string GetPic(string s, string format)
{
try
{
if (File.Exists(HttpContext.Current.Server.MapPath(s)))
{
return format.Replace("{0}", s);
}
return "";
}
catch
{
return "";
}
}
public static string GetPic(string s, string nopic, string format)
{
try
{
if (string.IsNullOrEmpty(nopic))
{
nopic = "images/nopic.jpg";
}
string path = HttpContext.Current.Server.MapPath(ApplicationPath + s);
if (!string.IsNullOrEmpty(format))
{
if (File.Exists(path))
{
return format.Replace("{0}", ApplicationPath + s);
}
return format.Replace("{0}", ApplicationPath + nopic);
}
if (File.Exists(path))
{
return (ApplicationPath + s);
}
return (ApplicationPath + nopic);
}
catch
{
if (!string.IsNullOrEmpty(format))
{
return format.Replace("{0}", ApplicationPath + nopic);
}
return (ApplicationPath + nopic);
}
}
public static string GetTitle(string str, int n)
{
if (str.Length > n)
{
str = str.Substring(0, n);
}
return str;
}
public static string parseHtml(string html)
{
return Regex.Replace(html, "<[^>]*>", "").Replace("&nbsp;", " ");
}
public static bool SelectComBox(DropDownList drop, int value)
{
return SelectComBox(drop, value.ToString());
}
public static bool SelectComBox(DropDownList drop, string value)
{
drop.ClearSelection();
if (drop.Items.FindByValue(value) != null)
{
drop.Items.FindByValue(value).Selected = true;
}
else
{
return false;
}
return true;
}
public static bool SelectComBox(RadioButtonList drop, int value)
{
return SelectComBox(drop, value.ToString());
}
public static bool SelectComBox(RadioButtonList drop, string value)
{
drop.ClearSelection();
if (drop.Items.FindByValue(value) != null)
{
drop.Items.FindByValue(value).Selected = true;
}
else
{
drop.Items[0].Selected = true;
return false;
}
return true;
}
}
}