写在前面:
此文用于收藏常用的方法。希望大家多多交流。
============================================================================================
1.截取指定长度的字符串并在末尾加入指定字符
05 |
public static string StringTruncat( string oldStr, int maxLength, string endWith) |
07 |
if ( string .IsNullOrEmpty(oldStr)) |
09 |
return oldStr + endWith; |
13 |
throw new Exception( "返回的字符串长度必须大于0" ); |
15 |
if (oldStr.Length>maxLength) |
17 |
string strTmp = oldStr.Substring(0, maxLength); |
18 |
if ( string .IsNullOrEmpty(endWith)) |
23 |
return strTmp + endWith; |
2.验证码生成方法
首先在html页面中加入javascript,此处注意imgNode.src必须根据情况自行修改。然后,新建一个“一般处理程序”,将WaterMark类的所有代码全部复制进去即可。
01 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
05 |
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" /> |
06 |
< script type = "text/javascript" > |
08 |
var imgNode = document.getElementById("vimg"); |
09 |
imgNode.src = "WaterMark.ashx?t=" + (new Date()).valueOf(); // 这里加个时间的参数是为了防止浏览器缓存的问题 |
14 |
< img src = "WaterMark.ashx" id = "vimg" alt = "" onclick = "change();" /> |
01 |
<%@ WebHandler Language= "C#" Class= "WaterMark" %> |
06 |
using System.Drawing.Drawing2D; |
07 |
using System.Web.SessionState; |
09 |
public class WaterMark : IHttpHandler, IRequiresSessionState |
12 |
public void ProcessRequest(HttpContext context) |
14 |
string checkCode = GenCode(5); |
15 |
context.Session[ "Code" ] = checkCode; |
16 |
System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22); |
17 |
Graphics g = Graphics.FromImage(image); |
21 |
Random random = new Random(); |
28 |
for (i = 0; i < 25; i++) |
30 |
int x1 = random.Next(image.Width); |
31 |
int x2 = random.Next(image.Width); |
32 |
int y1 = random.Next(image.Height); |
33 |
int y2 = random.Next(image.Height); |
34 |
g.DrawLine( new Pen(Color.Silver), x1, y1, x2, y2); |
37 |
Font font = new System.Drawing.Font( "Arial" , 12, (System.Drawing.FontStyle.Bold)); |
38 |
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true ); |
39 |
g.DrawString(checkCode, font, brush, 2, 2); |
42 |
g.DrawRectangle( new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); |
43 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
44 |
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); |
45 |
context.Response.ClearContent(); |
46 |
context.Response.ContentType = "image/Gif" ; |
47 |
context.Response.BinaryWrite(ms.ToArray()); |
59 |
/// <param name="num">随机出几个字符</param> |
60 |
/// <returns>随机出的字符串</returns> |
61 |
private string GenCode( int num) |
63 |
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; |
64 |
char [] chastr = str.ToCharArray(); |
67 |
Random rd = new Random(); |
69 |
for (i = 0; i < num; i++) |
72 |
code += str.Substring(rd.Next(0, str.Length), 1); |
78 |
public bool IsReusable |
3.无刷新实现弹窗
1 |
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message" , "<script language='javascript' defer>alert('加入暂存架成功!');</script>" ); |
4.MD5转码
1 |
using System.Web.Security; |
3 |
string Password = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text.ToString(), "MD5" ); |
(责任编辑:admin) |