开发者

Aspx only loaded 1 time

I'm trying to implement this Captcha and since it doesn't have an option to generate a new image I'm trying to build one and I'm doing this:

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 = "<table><tr><td>" });
            p1.Controls.Add(l);
            p1.Controls.Add(new Literal() { Text = "<br/>" });
            p1.Controls.Add(img);
            p1.Controls.Add(new Literal() { Text = "<br/>" });
            p1.Controls.Add(tb);
            p1.Controls.Add(new Literal() { Text = "<br/>" });
            p1.Controls.Add(linkB);
            p1.Controls.Add(new Literal() { Text = "</td></tr></table>" });
            return p1;
        }

   开发者_JAVA百科     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";
        }

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?

Update: I'm adding elements dynamically to a page, one of them is a captcha with the code available in this link Captcha , 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.

The code that generate the image is this:

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<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();
}
} 


There's not enough information in your question. Please post the full code of your page.

That said, with the information given, I suspect either:

  • Your browser is caching the HTML after the page loads the first time or
  • AutoEventWireup is not enabled and thus page_load is not called

There are a number of ways to prevent the browser caching the output of Turing.aspx.


If you have autoeventwireup turned on in the header of the .aspx page, then it will automatically run the page load event if it has the correct method signature.

If you have autoeventwireup turned off, you need to change your method to this:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    // Your code goes here.
    // ...
}

Make sure your method signature is correct, or override the base page class OnLoad event.

One more thing. In your onload event handler, you may have code that reference the IsPostBack Page property. make sure you're NOT doing this if you want the code to get called on postback:

if(!IsPostBack)
{
    // Code that wont get called on postback
    // ...
}

One final thing. It might make sense to just use a prebuilt captcha, instead of a homegrown one. I would recommend reCAPTCHA. VERY easy to integrate with an ASP.NET application.

UPDATE

I don't know if resetting the ImageUrl is going to refresh the image. This is not the solution, but try adding a querystring parameter to the ImageUrl on postback, just to see if the image is being cached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜