⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systemoperation.cs

📁 办公室系统包括上下班打卡
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public class SystemOperation
{
	/// <summary>
	/// 弹出一个对话框,并在对话框中显示msg信息
	/// </summary>
	/// <param name="response"></param>
	/// <param name="msg"></param>
	public static void OpenDialog(HttpResponse response,string msg)
	{
		response.Write("<script>alert('" + msg + "')</script>");
	}

	/// <summary>
	/// 检查str是否为空,如果为空,则返回true,否则返回false
	/// </summary>
	/// <param name="str"></param>
	/// <returns></returns>
	public static bool StringNullChecked(string str)
	{
		if(str == null || str == "" || str.Length < 1)
		{
			return true;
		}
		return false;
	}

	/// <summary>
	/// 选择值为value的选项为list控件的选择项
	/// </summary>
	/// <param name="list"></param>
	/// <param name="value"></param>
	public static void SetListSelected(DropDownList list,string value)
	{
		if(list.Items.Count <= 0)
		{
			list.SelectedIndex = -1;
			return;
		}
		///找到匹配项,并设置
		for(int i = 0; i < list.Items.Count; i++)
		{
			if(list.Items[i].Value == value)
			{
				list.SelectedIndex = i;
				return;
			}
		}
		list.SelectedIndex = -1;
	}

	/// <summary>
	/// 选择值为value的选项为list控件的选择项
	/// </summary>
	/// <param name="list"></param>
	/// <param name="value"></param>
	public static void SetListSelected(ListBox list,string value)
	{
		if(list.Items.Count <= 0)
		{
			list.SelectedIndex = -1;
			return;
		}
		///找到匹配项,并设置
		for(int i = 0; i < list.Items.Count; i++)
		{
			if(list.Items[i].Value == value)
			{
				list.SelectedIndex = i;
				return;
			}
		}
		list.SelectedIndex = -1;
	}

	/// <summary>
	/// 选择文本为text的选项为list控件的选择项
	/// </summary>
	/// <param name="list"></param>
	/// <param name="text"></param>
	public static void SetListSelectedText(DropDownList list,string text)
	{
		if(list.Items.Count <= 0)
		{
			list.SelectedIndex = -1;
			return;
		}
		///找到匹配项,并设置
		for(int i = 0; i < list.Items.Count; i++)
		{
			if(list.Items[i].Text == text)
			{
				list.SelectedIndex = i;
				return;
			}
		}
		list.SelectedIndex = -1;
	}

	/// <summary>
	/// 选择文本为text的选项为list控件的选择项
	/// </summary>
	/// <param name="list"></param>
	/// <param name="text"></param>
	public static void SetListSelectedText(ListBox list,string text)
	{
		if(list.Items.Count <= 0)
		{
			list.SelectedIndex = -1;
			return;
		}
		///找到匹配项,并设置
		for(int i = 0; i < list.Items.Count; i++)
		{
			if(list.Items[i].Text == text)
			{
				list.SelectedIndex = i;
				return;
			}
		}
		list.SelectedIndex = -1;
	}

	/// <summary>
	/// 获取url中"?"之前的字符串地址,即不携带参数的url
	/// </summary>
	/// <param name="url"></param>
	/// <returns></returns>
	public static string FormatErrorUrl(string url)
	{
		if(url.IndexOf("?") > 0)
		{
			return(url.Substring(0,url.IndexOf("?")));
		}
		return(url);
	}

	/// <summary>
	/// 返回错误处理页面,并在地址中携带错误消息和错误页面的地址
	/// </summary>
	/// <param name="url"></param>
	/// <param name="message"></param>
	/// <returns></returns>
	public static string FormatErrorPageUrl(string url,string message)
	{
		return ("~/Common/ErrorPage.aspx?ErrorMsg=" + message
			+ "&ErrorUrl=" + url.Replace("\n","").Replace("\t",""));
	}

	/// <summary>
	/// 根据当前时间创建一个字符串,该字符串由时间的
	/// 年、月、日、时、分、秒、毫秒字符串构成
	/// </summary>
	/// <returns></returns>
	public static string CreateFileNameByDateTime()
	{
		DateTime now = DateTime.Now;
		///构建字符串
		string fileName = now.Year.ToString()
			+ now.Month.ToString()
			+ now.Day.ToString()
			+ now.Hour.ToString()
			+ now.Minute.ToString()
			+ now.Second.ToString()
			+ now.Millisecond.ToString();
		return (fileName);
	}

	/// <summary>
	/// 返回指定长度的字符串,如果字符串超过指定的长度,则显示"...",
	/// 同时还处理了中文字符
	/// </summary>
	/// <param name="str"></param>
	/// <param name="length"></param>
	/// <returns></returns>
	public static string FormatStringLength(string str,int length)
	{
		if(SystemOperation.StringNullChecked(str) == false)
		{
			return string.Empty;
		}
		///如果包含中文字符,中文字符的长度加倍
		if(Encoding.UTF8.GetByteCount(str) > str.Length)
		{   ///调整为中文的长度,等于英文的一半
			length = length / 2;
		}
		if(str.Length > length)
		{
			return str.Substring(0,length)
				+ "...";
		}
		return str;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -