最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
三款asp.net 验证码代码
时间:2010-08-26 编辑:简简单单 来源:一聚教程网
< /script>//asp.net中避免页面多次提交的代码:asp.netpublic class preventmulticlick : system.web.ui.page {
protected system.web.ui.webcontrols.button button1; protected system.web.ui.webcontrols.button button2;
protected system.web.ui.webcontrols.linkbutton linkbutton1; protected system.web.ui.webcontrols.button button3; private void page_load(object sender, system.eventargs e)
{
this.getpostbackeventreference(this.button3);
//保证 __dopostback(eventtarget, eventargument) 精确注册 if(!ispostback)
{
system.text.stringbuilder sb = new system.text.stringbuilder();
sb.append("if (typeof(page_clientvalidate) == 'function')
{
if (page_clientvalidate() == false)
{
return false;
}
}"); //保证考证函数的执行 sb.append("if(window.confirm('are you sure?')==false) return false; ");
//自定义客户端脚本 sb.append("disableothersubmit(); ");
// disable一切submit按钮 sb.append(this.getpostbackeventreference(this.button3));
//用__dopostback来提交,保证按钮的效劳器端click工作执行 sb.append("; ");
button3.attributes.add("onclick",sb.tostring());
}
} #region web form designer generated code override protected void oninit(eventargs e)
{
// // codegen: this call is required by the asp.net web form designer. // initializecomponent();
base.oninit(e);
}
/// < summary> /// required method for designer support - do not modify /// the contents of this method with the code editor. /// < /summary> private void initializecomponent()
{
this.button3.click += new system.eventhandler(this.button3_click); this.load += new system.eventhandler(this.page_load);
}
#endregion private void button3_click(object sender, system.eventargs e)
{
system.threading.thread.sleep(3000);
response.write("hello world!");
}
} 此处只是disable掉一切的submit button, 我感觉其它的可提交控件也是能够经由相似的办法来disable的.
1、在web项目中添加一个类,如"createimage.cs",然后将我公布的源代码copy进去;
2、再新建一个web窗体,如"image.aspx",在page_load中加入代码 "createimage.drawimage ();"当然别忘了加上对类的引用哦!!
3、在页面的合适位置上(你想放验证码的位置)上加上如下javascript代码就ok 了,
///
/// 验证码模块
///
public class createimage
{
public static void drawimage()
{
createimage img=new createimage();
httpcontext.current.session["checkcode"]=img.rndnum(4);
img.createimages(httpcontext.current.session["checkcode"].tostring());
}
///
/// 生成验证图片
///
/// 验证字符
private void createimages(string checkcode)
{
int iwidth = (int)(checkcode.length * 13);
system.drawing.bitmap image = new system.drawing.bitmap(iwidth, 23);
graphics g = graphics.fromimage(image);
g.clear(color.white);
//定义颜色
color[] c = {color.black,color.red,color.darkblue,color.green,color.orange,color.brown,color.darkcyan,color.purple};
//定义字体
string[] font = {"verdana","microsoft sans serif","comic sans ms","arial","宋体"};
random rand = new random();
//随机输出噪点
for(int i=0;i<50;i++)
{
int x = rand.next(image.width);
int y = rand.next(image.height);
g.drawrectangle(new pen(color.lightgray, 0),x,y,1,1);
}
//输出不同字体和颜色的验证码字符
for(int i=0;i
{
int cindex = rand.next(7);
int findex = rand.next(5);
font f = new system.drawing.font(font[findex], 10, system.drawing.fontstyle.bold);
brush b = new system.drawing.solidbrush(c[cindex]);
int ii=4;
if((i+1)%2==0)
{
ii=2;
}
g.drawstring(checkcode.substring(i,1), f, b, 3+(i*12), ii);
}
//画一个边框
g.drawrectangle(new pen(color.black,0),0,0,image.width-1,image.height-1);
//输出到浏览器
system.io.memorystream ms = new system.io.memorystream();
image.save(ms,system.drawing.imaging.imageformat.jpeg);
httpcontext.current.response.clearcontent();
//response.clearcontent();
httpcontext.current.response.contenttype = "image/jpeg";
httpcontext.current.response.binarywrite(ms.toarray());
g.dispose();
image.dispose();
}
///
/// 生成随机的字母
///
/// 生成字母的个数
/// string
private string rndnum(int vcodenum)
{
string vchar = "0,1,2,3,4,5,6,7,8,9" ;
string[] vcarray = vchar.split(',') ;
string vnum = "" ; //由于字符串很短,就不用stringbuilder了
int temp = -1 ; //记录上次随机数值,尽量避免生产几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
random rand =new random();
for ( int i = 1 ; i < vcodenum+1 ; i++ )
{
if ( temp != -1)
{
rand =new random(i*temp*unchecked((int)datetime.now.ticks));
}
int t = rand.next(vcarray.length ) ;
if (temp != -1 && temp == t)
{
return rndnum( vcodenum );
}
temp = t ;
vnum += vcarray[t];
}
return vnum ;
}
%>