asp.net ajax实现无刷新验证码
程序员文章站
2024-03-07 19:05:15
1、首先是在后台验证码的aspx文件的page_load中的事件代码: 复制代码 代码如下: using system; using system.collections....
1、首先是在后台验证码的aspx文件的page_load中的事件代码:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.drawing;
namespace 学生在线考试系统
{
public partial class ajaxautocode : system.web.ui.page
{
//验证数字
public string authcode = string.empty;
protected void page_load(object sender, eventargs e)
{
#region 第一种产生验证码的方法
random random = new random();
authcode = random.next(1111, 9999).tostring();
//构造图片
bitmap image = new bitmap(authcode.length * 12, 25);
//创建画布
graphics g = graphics.fromimage(image);
try
{
g.clear(color.white);
for (int i = 0; i < 25; i++)
{
int x1 = random.next(image.width);
int x2 = random.next(image.width);
int y1 = random.next(image.height);
int y2 = random.next(image.height);
//链接两点的线条
g.drawline(new pen(color.silver), x1, y1, x2, y2);
}
font font = new font("arial", 12, fontstyle.bold | fontstyle.italic);
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(
new rectangle(0, 0, image.width, image.height), color.blue, color.darkblue, 1.2f, true);
g.drawstring(authcode, font, brush, 2, 2);
//画图片的前景噪点
for (int i = 0; i < 100; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.gif);
ms.writeto(this.response.outputstream);
ms.close();
this.response.contenttype = "image/gif";
}
finally
{
image.dispose();
g.dispose();
}
#endregion
}
}
}
2、其次在显示验证码的页面定义一个js函数
function fgetcode()
{
document.getelementbyid("getcode").src="default2.aspx?"+math.random();
}
3.再编辑前台页面aspx,下面是前台页面的代码片段
<label>验证码</label>
<asp:textbox id="txt_checkcode" runat="server" width="178px"></asp:textbox>
<img src="default2.aspx" alt="看不清楚?" id="getcode"/> <a href="javascript:fgetcode()">更换验证码</a>
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.drawing;
namespace 学生在线考试系统
{
public partial class ajaxautocode : system.web.ui.page
{
//验证数字
public string authcode = string.empty;
protected void page_load(object sender, eventargs e)
{
#region 第一种产生验证码的方法
random random = new random();
authcode = random.next(1111, 9999).tostring();
//构造图片
bitmap image = new bitmap(authcode.length * 12, 25);
//创建画布
graphics g = graphics.fromimage(image);
try
{
g.clear(color.white);
for (int i = 0; i < 25; i++)
{
int x1 = random.next(image.width);
int x2 = random.next(image.width);
int y1 = random.next(image.height);
int y2 = random.next(image.height);
//链接两点的线条
g.drawline(new pen(color.silver), x1, y1, x2, y2);
}
font font = new font("arial", 12, fontstyle.bold | fontstyle.italic);
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(
new rectangle(0, 0, image.width, image.height), color.blue, color.darkblue, 1.2f, true);
g.drawstring(authcode, font, brush, 2, 2);
//画图片的前景噪点
for (int i = 0; i < 100; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.gif);
ms.writeto(this.response.outputstream);
ms.close();
this.response.contenttype = "image/gif";
}
finally
{
image.dispose();
g.dispose();
}
#endregion
}
}
}
2、其次在显示验证码的页面定义一个js函数
复制代码 代码如下:
function fgetcode()
{
document.getelementbyid("getcode").src="default2.aspx?"+math.random();
}
3.再编辑前台页面aspx,下面是前台页面的代码片段
复制代码 代码如下:
<label>验证码</label>
<asp:textbox id="txt_checkcode" runat="server" width="178px"></asp:textbox>
<img src="default2.aspx" alt="看不清楚?" id="getcode"/> <a href="javascript:fgetcode()">更换验证码</a>
下一篇: java编程实现邮件定时发送的方法