Aspx仅加载1次

时间:2010-03-22 13:26:09

标签: c# asp.net

我正在尝试实现这个Captcha,因为它没有生成新图像的选项,我正在尝试构建一个,我正在这样做:

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

        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";
        }

正在发生的事情是,当我试图生成一个新的图像时,它不会对“Turing.aspx”的page_load做任何想法吗?

更新:我正在向页面动态添加元素,其中一个是带有此链接Captcha中可用代码的验证码,我所做的唯一更改是动态创建验证码元素请参阅“私有Panel buildCaptchaElement(XmlNode节点)”并添加链接按钮以生成新图像。

生成图像的代码是:

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

2 个答案:

答案 0 :(得分:0)

您的问题中没有足够的信息。请发布您网页的完整代码。

那就是说,根据所提供的信息,我怀疑:

  • 首次加载页面后,您的浏览器正在缓存HTML
  • AutoEventWireup未启用,因此未调用page_load

有多种方法prevent the browser caching Turing.aspx的输出。

答案 1 :(得分:0)

如果您在.aspx页面的标题中打开了autoeventwireup,那么如果它具有正确的方法签名,它将自动运行页面加载事件。

如果您关闭了autoeventwireup,则需要将方法更改为:

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

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

确保您的方法签名正确,或覆盖基页类OnLoad事件。

还有一件事。在onload事件处理程序中,您可能拥有引用IsPostBack Page属性的代码。如果您希望在回发时调用代码,请确保您这样做:

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

最后一件事。使用预先构建的验证码而不是本土的验证码可能是有意义的。我建议reCAPTCHA。非常容易与ASP.NET应用程序集成。

<强>更新

我不知道重置ImageUrl是否会刷新图​​像。这不是解决方案,但尝试在回发时向ImageUrl添加查询字符串参数,只是为了查看图像是否正在被缓存。