日期:2014-05-18  浏览次数:20393 次

怎样让验证字符串图片刷新,而不是整个页面刷新呢?
<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.Fon