![](/img/trans.png)
[英]Why clicking an ImageButton in an Aspx page works only at second time?
[英]Aspx only loaded 1 time
我想實現這個驗證碼 ,並因為它沒有一個選項,以生成一個新的形象,我試圖建立一個與我這樣做:
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 ,我所做的唯一更改是為驗證碼動態創建了元素,如您在“專用面板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();
}
}
您的問題中沒有足夠的信息。 請發布您頁面的完整代碼。
也就是說,根據給出的信息,我懷疑:
如果您在.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將刷新圖像。 這不是解決方案,但嘗試在回發時將querystring參數添加到ImageUrl,只是查看是否正在緩存圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.