首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎的让验证字符串图片刷新,而不是整个页面刷新呢

2011-12-18 
怎样让验证字符串图片刷新,而不是整个页面刷新呢?TRTDvAlign middle align right height 20

怎样让验证字符串图片刷新,而不是整个页面刷新呢?
<TR>
      <TD   vAlign= "middle "   align= "right "   height= "20 "> 输入验证码: </TD>
      <TD   colSpan= "2 "   valign= "middle ">
      <asp:TextBox   ID= "tbx_code "   Runat= "server "   Width= "80px "> </asp:TextBox>
      <img   id= "validateimg "   src= "validate.aspx ">
      看不清楚,换一张
      </TD>
</TR>
我怎么通过点击“看不清楚,换一张”来刷新一下验证图片,但不让整个页面刷新呢?
大家帮帮忙,分不够,我再加!拜托了

[解决办法]
1:弄个IFRAME

2:弄个DIV

3:AJAX改变图片路径
[解决办法]
防止刷新对于.net程序员有4种方法:
js控制,继承icallbackeventhandle,把要更新的放到iframe下,放到ajax控件下.
选一种吧.
[解决办法]
<img id= "validateimg " src= "validate.aspx ">
<a href= "# " onclick= "changepic() "> 看不清楚,换一张 </a>

function changepic()
{
document.getElementById( "validateimg ").src= "validate.aspx? " + Math.random();
}

[解决办法]
没必要用Ajax,只要JS就能解决了

首先新建一个页面如Validate.aspx
然后把生成验证码的代码写入到这个页面中生成,生成以后加入this.Response.Redirect( "刚才生成的验证码路径? " + Guid.NewGuid().ToString());

然后在要使用验证码的页面这样写Html

<img id= "Image1 " alt= "点击更换图片 " onclick= "this.src= 'Validate.aspx? '+Math.random(); "
src= "Validate.aspx " style= "width: 50px; cursor: pointer; height: 16px " />

如果还需要点击一个超连接来切换则加入
<a href= '# ' onclick= 'document.getElementById( "Image1 ").src= "Validate.aspx? "+Math.random(); '> 看不清?换一张 </a>
[解决办法]
1、生成页面 CheckCode.aspx
代码:
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;

System.Random random = new Random();

for(int i=0; i <5; i++)
{
number = random.Next();

if(number % 2 == 0)
code = (char)( '0 ' + (char)(number % 10));
else
code = (char)( 'A ' + (char)(number % 26));

checkCode += code.ToString();
}

Response.Cookies.Add(new HttpCookie( "CheckCode ", checkCode));

return checkCode;
}

private void CreateCheckCodeImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;

System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);

try
{
//生成随机生成器
Random random = new Random();

//清空图片背景色
g.Clear(Color.White);

//画图片的背景噪音线
for(int i=0; i <15; 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 System.Drawing.Font( "Arial ", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));


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);
g.DrawString(checkCode, font, brush, 2, 2);

//画图片的前景噪音点
for(int i=0; i <25; 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);
Response.ClearContent();
Response.ContentType = "image/Gif ";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

2、登录页面代码:
private void Image1_ServerClick(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Request.Cookies[ "CheckCode "] == null)
{
lblMessage.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。 ";
lblMessage.Visible = true;
return;
}

if(String.Compare(Request.Cookies[ "CheckCode "].Value, txtCheckCode.Text, true) != 0)
{
lblMessage.Text = "验证码错误,请输入正确的验证码! ";
lblMessage.Visible = true;
return;

}
else
{
Response.Redirect( "Login.aspx ");
}
}

3、登录页面中添加一个控件引用CheckCode.aspx页面,HTML代码:
<P>
<IMG id= "Img2 " height= "23 " src= "CheckCode.aspx " width= "72 " runat= "server " alt= "点击刷新 "
onclick= "this.src=this.src+ '? ' "> <a> <FONT color= "#669966 "> 点图换一张! </FONT> </a> </FONT> </P>

热点排行