Aspx only loaded 1 time
<p>I'm trying to implement this <a href="http://www.c-sharpcorner.com/UploadFile/oguzaltuncu/TextToImageGen08012005023259AM/TextToImageGen.aspx" rel="nofollow">Captcha</a> and since it doesn't have an option to generate a new image I'm trying to build one and I'm doing this:</p>

<pre><code>private Panel buildCaptchaElement(XmlNode node)
        {
            Panel p1 = new Panel();
            Label l = new Label();
            l.Text = node.ChildNodes[3].InnerText;
            TextBox tb = new TextBox();
            tb.ID = "Captcha_Tb";

            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
            img.ID = "Captcha_Img";
            img.ImageUrl = this.ResolveUrl("Turing.aspx");
            img.Width = new Unit(140, UnitType.Pixel);
            img.Height = new Unit(70, UnitType.Pixel);

            LinkButton linkB = new LinkButton();
            linkB.ID = "Captcha_linkB";
            linkB.Text = "Can't read? Generate a new image.";
            linkB.Click += new EventHandler(linkB_Click);

            p1.Controls.Add(new Literal() { Text = "&lt;table&gt;&lt;tr&gt;&lt;td&gt;" });
            p1.Controls.Add(l);
            p1.Controls.Add(new Literal() { Text = "&lt;br/&gt;" });
            p1.Controls.Add(img);
            p1.Controls.Add(new Literal() { Text = "&lt;br/&gt;" });
            p1.Controls.Add(tb);
            p1.Controls.Add(new Literal() { Text = "&lt;br/&gt;" });
            p1.Controls.Add(linkB);
            p1.Controls.Add(new Literal() { Text = "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;" });
            return p1;
        }

        void linkB_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Image img = FindControl("Captcha_Img") as System.Web.UI.WebControls.Image;
            img.ImageUrl = "Turing.aspx";
        }
</code></pre>

<p>What's happening is that when I'm trying to generate a new Image it doesn't do the page_load of "Turing.aspx" any idea why?</p>

<p>Update: I'm adding elements dynamically to a page, one of them is a captcha with the code available in this link <a href="http://www.c-sharpcorner.com/UploadFile/oguzaltuncu/TextToImageGen08012005023259AM/TextToImageGen.aspx" rel="nofollow">Captcha</a> , the only changes i made was the creation of elements for the captcha dynamically as you see in "private Panel buildCaptchaElement(XmlNode node)" and a add the link button to generate a new image.</p>

<p>The code that generate the image is this:</p>

<pre><code>public class Turing1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Bitmap objBMP =new System.Drawing.Bitmap(60,20);
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Green);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr="";
int[] myIntArray = new int[5] ;
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x=0;x&lt;5;x++)
{
myIntArray[x] = System.Convert.ToInt32 (autoRand.Next(0,9));
randomStr+= (myIntArray[x].ToString ());
}
//This is to add the string to session cookie, to be compared later
Session.Add("randomStr",randomStr);
//' Write out the text
objGraphics.DrawString(randomStr, 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>
