CompareValidator ValueToCompare

时间:2012-03-24 21:05:14

标签: asp.net

我正在从页面captcha.aspx生成验证码图像。它将“结果”存储在会话变量Session.Add("randomString", result);中。在我的Web表单中,我使用CompareValidator将用户输入的值与会话值进行比较。图像显示正确,但是,在页面加载时,Session变量似乎为null。可能是什么问题?

Captcha.aspx.cs

//Removed the preceding code since it just adds the string
//This is to add the string to session cookie, to be compared later
Session.Add("randomStr", result);
//Write out the text
objGraphics.DrawString(expression, objFont, Brushes.White, 3, 3);
//Set the content type and return the image
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();

Registration.aspx.cs

string captcha = Session["randomStr"] as String;
captchaC.ValueToCompare = captcha;

Registration.aspx

<asp:CompareValidator ID="captchaC" runat="server" ControlToValidate="txtCaptcha"
ErrorMessage="Invalid captcha" Display="None" Type="String"></asp:CompareValidator>

0 个答案:

没有答案