CompareValidator ValueToCompare
<p>I'm having a captcha image generated from a page <code>captcha.aspx</code>. It stores the "result" in a Session variable <code>Session.Add("randomString", result);</code>. In my web form I am using a CompareValidator to compare the value entered by the user with the session value. The image displays correctly, however, on page load the Session variable seems to be null. What may be the problem?</p>

<p><strong>Captcha.aspx.cs</strong></p>

<pre><code>//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();
</code></pre>

<p><strong>Registration.aspx.cs</strong></p>

<pre><code>string captcha = Session["randomStr"] as String;
captchaC.ValueToCompare = captcha;
</code></pre>

<p><strong>Registration.aspx</strong></p>

<pre><code>&lt;asp:CompareValidator ID="captchaC" runat="server" ControlToValidate="txtCaptcha"
ErrorMessage="Invalid captcha" Display="None" Type="String"&gt;&lt;/asp:CompareValidator&gt;
</code></pre>
