asp.net(C#) 常用算法(一)
来源:互联网 发布:java文件上传代码 编辑:程序博客网 时间:2024/05/18 01:01
做 ASP.NET 程序的时候,常常要重复许多代码,如果把它们抽取出来,能极大的提高开发效率,整理了一些自己常用的代码,算法贴出来,以后再补充。
using System;
using System.Web.Security;
/// <summary>
///
/// ** asp.net(C#) 常用算法 **
///
/// File: Common.cs
///
/// Author: 周振兴 (Zxjay 飘遥)
///
/// E-Mail: tda7264@163.com
///
/// Date: 07-05-19
///
/// http://blog.csdn.net/zxjay/
///
/// </summary>
public class Common
{
private static string CurrentUser = string.Empty;
/// <summary>
/// ASP.NET 常用算法
/// </summary>
public Common()
{
}
/// <summary>
/// 获取或设置当前用户名称
/// </summary>
public static string GetCurrentUser
{
set { CurrentUser = value; }
get { return CurrentUser; }
}
/// <summary>
/// 限制字符串长度,清除特殊字符
/// </summary>
/// <param name="str"> 待处理的字符串 </param>
/// <param name="maxlength"> 最大长度 </param>
/// <returns> 处理后的字符串 </returns>
public static string ClearString(string str,int maxlength)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
str=str.Trim();
if(str==string.Empty)
{
return string.Empty;
}
if(str.Length>maxlength)
{
str=str.Substring(0,maxlength);
}
str=str.Replace("'","''");
return str;
}
/// <summary>
/// 密码字符串散列加密( MD5算法 )
/// </summary>
/// <param name="inputstr"> 待加密的字符串 </param>
/// <returns> 加密后字符串 </returns>
public static string MD5(string inputstr)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(inputstr, "MD5").
ToLower().Substring(0, 15);
}
/// <summary>
/// 插入Flash或图像
/// </summary>
/// <param name="filename"> 媒体文件名称 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <returns> 能展示媒体的 HTML 代码 </returns>
public static string ShowMedia(string filename, int width, int height)
{
string fname = filename.Trim();
if (filename == string.Empty)
{
return string.Empty;
}
string fileext = fname.Substring(fname.LastIndexOf("."));
//flash文件
if (fileext.ToLower() == ".swf")
{
string swfObject = @"<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'
codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'
width='" + width + @"' height='" + height + @"'>
<param name='movie' value='" + filename + @"' />
<param name='quality' value='high' />
<param name='wmode' value='transparent'>
<embed src='" + filename + @"' quality='high'
pluginspage='http://www.macromedia.com/go/getflashplayer'
type='application/x-shockwave-flash' width='" + width + @"' height='" + height + @"'></embed>
</object> ";
return swfObject;
}
//图像文件
else
{
string imgObject = @"<img alt='' width='" + width + @"px' height='" + height +
@"px' src='" + filename + @"' />";
return imgObject;
}
}
/// <summary>
/// 弹出提示框
/// </summary>
/// <param name="message"> 提示信息 </param>
/// <returns> 提示框的JS代码 </returns>
public static string ShowMessage(string message)
{
return "<script>alert('" + message + "');</script>";
}
/// <summary>
/// 弹出提示框并且重定向到另一链接
/// </summary>
/// <param name="message"> 提示信息 </param>
/// <param name="urlToRedirect"> 重定向到的链接 </param>
/// <returns> 提示框的JS代码 </returns>
public static string ShowMessageAndRedirect(string message, string urlToRedirect)
{
return "<script>alert('" + message + "');document.location.href='" + urlToRedirect + "'</script>";
}
/// <summary>
/// 打开可调节大小的窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <param name="locationX"> 距屏幕左端的距离 </param>
/// <param name="locationY"> 据屏幕上端的距离 </param>
/// <returns> 可调节大小的窗口JS代码 </returns>
public static string OpenResizableWindow(string url, int width, int height, int locationX, int locationY)
{
string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=yes,
scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no";
return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY));
}
/// <summary>
/// 打开固定大小的窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <param name="locationX"> 距屏幕左端的距离 </param>
/// <param name="locationY"> 据屏幕上端的距离 </param>
/// <returns> 固定大小的窗口JS代码 </returns>
public static string OpenFixedWindow(string url, int width, int height, int locationX, int locationY)
{
string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=no,
scrollbars=no, status=no, titlebar=no, toolbar=no, directories=no";
return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY));
}
/// <summary>
/// 打开窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="sFeatures"> 定义窗口格式 </param>
/// <returns> 打开窗口的JS代码 </returns>
public static string OpenWindow(string url, string sFeatures)
{
string jsString = @"<script type='text/javascript'>" +
@"window.open('{0}','','{1}');"+
@"</script>";
return string.Format(jsString, url, sFeatures);
}
/// <summary>
/// 打开可调节大小的模态对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 模态对话框宽度 </param>
/// <param name="height"> 模态对话框高度 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowResizableModalDialog(string url, int width, int height)
{
string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes;resizable:yes;status:yes;
scroll:yes;edge:sunken; ";
return ShowModalDialog(url, string.Format(sFeatures, width, height));
}
/// <summary>
/// 打开固定大小的模态对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 模态对话框宽度 </param>
/// <param name="height"> 模态对话框高度 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowFixedModalDialog(string url, int width, int height)
{
string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes;resizable:no;status:no;scroll:no;";
return ShowModalDialog(url, string.Format(sFeatures, width, height));
}
/// <summary>
/// 打开模式对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="sFeatures"> 定义模态对话框格式 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowModalDialog(string url, string sFeatures)
{
string jsString = @"<script type='text/javascript'>" +
@"showModalDialog('{0}','','{1}'" +
@"</script>";
return string.Format(jsString, url, sFeatures);
}
}
using System.Web.Security;
/// <summary>
///
/// ** asp.net(C#) 常用算法 **
///
/// File: Common.cs
///
/// Author: 周振兴 (Zxjay 飘遥)
///
/// E-Mail: tda7264@163.com
///
/// Date: 07-05-19
///
/// http://blog.csdn.net/zxjay/
///
/// </summary>
public class Common
{
private static string CurrentUser = string.Empty;
/// <summary>
/// ASP.NET 常用算法
/// </summary>
public Common()
{
}
/// <summary>
/// 获取或设置当前用户名称
/// </summary>
public static string GetCurrentUser
{
set { CurrentUser = value; }
get { return CurrentUser; }
}
/// <summary>
/// 限制字符串长度,清除特殊字符
/// </summary>
/// <param name="str"> 待处理的字符串 </param>
/// <param name="maxlength"> 最大长度 </param>
/// <returns> 处理后的字符串 </returns>
public static string ClearString(string str,int maxlength)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
str=str.Trim();
if(str==string.Empty)
{
return string.Empty;
}
if(str.Length>maxlength)
{
str=str.Substring(0,maxlength);
}
str=str.Replace("'","''");
return str;
}
/// <summary>
/// 密码字符串散列加密( MD5算法 )
/// </summary>
/// <param name="inputstr"> 待加密的字符串 </param>
/// <returns> 加密后字符串 </returns>
public static string MD5(string inputstr)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(inputstr, "MD5").
ToLower().Substring(0, 15);
}
/// <summary>
/// 插入Flash或图像
/// </summary>
/// <param name="filename"> 媒体文件名称 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <returns> 能展示媒体的 HTML 代码 </returns>
public static string ShowMedia(string filename, int width, int height)
{
string fname = filename.Trim();
if (filename == string.Empty)
{
return string.Empty;
}
string fileext = fname.Substring(fname.LastIndexOf("."));
//flash文件
if (fileext.ToLower() == ".swf")
{
string swfObject = @"<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'
codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'
width='" + width + @"' height='" + height + @"'>
<param name='movie' value='" + filename + @"' />
<param name='quality' value='high' />
<param name='wmode' value='transparent'>
<embed src='" + filename + @"' quality='high'
pluginspage='http://www.macromedia.com/go/getflashplayer'
type='application/x-shockwave-flash' width='" + width + @"' height='" + height + @"'></embed>
</object> ";
return swfObject;
}
//图像文件
else
{
string imgObject = @"<img alt='' width='" + width + @"px' height='" + height +
@"px' src='" + filename + @"' />";
return imgObject;
}
}
/// <summary>
/// 弹出提示框
/// </summary>
/// <param name="message"> 提示信息 </param>
/// <returns> 提示框的JS代码 </returns>
public static string ShowMessage(string message)
{
return "<script>alert('" + message + "');</script>";
}
/// <summary>
/// 弹出提示框并且重定向到另一链接
/// </summary>
/// <param name="message"> 提示信息 </param>
/// <param name="urlToRedirect"> 重定向到的链接 </param>
/// <returns> 提示框的JS代码 </returns>
public static string ShowMessageAndRedirect(string message, string urlToRedirect)
{
return "<script>alert('" + message + "');document.location.href='" + urlToRedirect + "'</script>";
}
/// <summary>
/// 打开可调节大小的窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <param name="locationX"> 距屏幕左端的距离 </param>
/// <param name="locationY"> 据屏幕上端的距离 </param>
/// <returns> 可调节大小的窗口JS代码 </returns>
public static string OpenResizableWindow(string url, int width, int height, int locationX, int locationY)
{
string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=yes,
scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no";
return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY));
}
/// <summary>
/// 打开固定大小的窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 宽度 </param>
/// <param name="height"> 高度 </param>
/// <param name="locationX"> 距屏幕左端的距离 </param>
/// <param name="locationY"> 据屏幕上端的距离 </param>
/// <returns> 固定大小的窗口JS代码 </returns>
public static string OpenFixedWindow(string url, int width, int height, int locationX, int locationY)
{
string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=no,
scrollbars=no, status=no, titlebar=no, toolbar=no, directories=no";
return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY));
}
/// <summary>
/// 打开窗口
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="sFeatures"> 定义窗口格式 </param>
/// <returns> 打开窗口的JS代码 </returns>
public static string OpenWindow(string url, string sFeatures)
{
string jsString = @"<script type='text/javascript'>" +
@"window.open('{0}','','{1}');"+
@"</script>";
return string.Format(jsString, url, sFeatures);
}
/// <summary>
/// 打开可调节大小的模态对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 模态对话框宽度 </param>
/// <param name="height"> 模态对话框高度 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowResizableModalDialog(string url, int width, int height)
{
string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes;resizable:yes;status:yes;
scroll:yes;edge:sunken; ";
return ShowModalDialog(url, string.Format(sFeatures, width, height));
}
/// <summary>
/// 打开固定大小的模态对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="width"> 模态对话框宽度 </param>
/// <param name="height"> 模态对话框高度 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowFixedModalDialog(string url, int width, int height)
{
string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes;resizable:no;status:no;scroll:no;";
return ShowModalDialog(url, string.Format(sFeatures, width, height));
}
/// <summary>
/// 打开模式对话框
/// </summary>
/// <param name="url"> 待打开的链接 </param>
/// <param name="sFeatures"> 定义模态对话框格式 </param>
/// <returns> 模态对话框的JS代码 </returns>
public static string ShowModalDialog(string url, string sFeatures)
{
string jsString = @"<script type='text/javascript'>" +
@"showModalDialog('{0}','','{1}'" +
@"</script>";
return string.Format(jsString, url, sFeatures);
}
}
- asp.net(C#) 常用算法(一)
- C#+ASP.NET常用代码
- asp.net的一些常用代码(一)
- Asp.net开发常用技巧收集一
- asp.net 开发常用的方法一
- ASP.NET(C#)常用代码30例
- ASP.NET(C#)常用代码30例
- ASP.NET(C#)常用代码30例
- Asp.net(C#)常用函数表
- Asp.net(C#)常用函数表
- Asp.net(C#)常用函数表
- ASP.NET(c#)常用类函数
- Asp.net(C#)常用函数表
- Asp.net(C#)常用函数表
- Asp.net(C#)常用函数表
- Asp.net(C#)常用函数表
- asp.net(c#)常用正则表达式实例
- ASP.NET常用代码(c#)
- Vector简概
- 简单测试一下
- ARM初始化
- 求救!!!一个关于C语言画图的题!!!谢谢!!!
- IT从业人员必看的10的论坛
- asp.net(C#) 常用算法(一)
- 增强现实Augmented Reality简介
- S3C2410快速启动的实现
- A little story
- 使用正则表达式定义Spring切入点
- 印度软件工程师:职场落魄,情场失意
- 改变人生的32句励志名言
- 对一个类的重构
- 值得珍藏的81句生活哲理名言