2009-12-31 157 views
4

動態的ImageButton點擊觸發的事件,請幫助我 創建在asp.net動態按鈕OnInit方法動態的ImageButton單擊事件不被解僱沒有得到

protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
ImageButton img = new ImageButton(); 
     img.ID = "first_button"; 
     img.Click += new ImageClickEventHandler(first_Click); 
     img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.first.gif"); 
     img.Attributes.Add("onmouseover", "onmousehand(this,'over')"); 
     img.Attributes.Add("onmouseout", "onmousehand(this,'out')"); 
     p1.Controls.Add(img); 
     img.Dispose(); 
     img = new ImageButton(); 
     img.ID = "previous_button"; 
     img.Click += new ImageClickEventHandler(previous_Click); 
     img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.previous.gif"); 
     img.Attributes.Add("onmouseover", "onmousehand(this,'over')"); 
     img.Attributes.Add("onmouseout", "onmousehand(this,'out')"); 
     p1.Controls.Add(img); 
     img.Dispose(); 

     t1.Attributes.Add("style", "color:#666666;"); 
     // t1.Text = "Page " + current_page + " of " + total_pages; 
     t1.ID = "text_box1"; 
     t1.Attributes.Add("onclick", "textbox_enable('" + t1.ClientID + "')"); 
     p1.Controls.Add(t1); 
     img = new ImageButton(); 
     img.ID = "go_button"; 
     img.Click += new ImageClickEventHandler(go_Click); 
     img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.go.GIF"); 
     img.Attributes.Add("onmouseover", "onmousehand(this,'over')"); 
     img.Attributes.Add("onmouseout", "onmousehand(this,'out')"); 
     p1.Controls.Add(img); 
     img.Dispose(); 
     ImageButton img1 = new ImageButton(); 
     img1.ID = "next_button"; 
     img1.CommandName = "next_button"; 
     img1.CommandArgument = "next1"; 

     img1.Click += new ImageClickEventHandler(next_Click); 
     img1.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.next.gif"); 
     img1.Attributes.Add("onmouseover", "onmousehand(this,'over')"); 
     img1.Attributes.Add("onmouseout", "onmousehand(this,'out')"); 
     p1.Controls.Add(img1); 
     //img.Dispose(); 
     img = new ImageButton(); 
     img.ID = "last_button"; 
     img.Click += new ImageClickEventHandler(last_Click); 
     img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ClassLibrary1.image.last.gif"); 
     img.Attributes.Add("onmouseover", "onmousehand(this,'over')"); 
     img.Attributes.Add("onmouseout", "onmousehand(this,'out')"); 
     p1.Controls.Add(img); 
     img.Dispose();  
    } 
    private void next_Click(object sender, ImageClickEventArgs e) 
    { 

     ImageButton next = (ImageButton)sender; 
     string value = next.CommandArgument; 
     current_page++; 
     t1.Text = "Page " + current_page + "of" + total_pages; 
    } 
    protected override void Render(HtmlTextWriter writer) 
    { 

     t1.Text = "Page " + current_page + " of " + total_pages; 
     p1.RenderControl(writer); 
     base.Render(writer); 
    } 

回答

1

試試這個

protected void Page_Load(object sender, EventArgs e) 
{ 
    ImageButton img = new ImageButton(); 
    img.ID = "SampleImage"; 
    img.Click += new ImageClickEventHandler(img_Click); 
    this.form1.Controls.Add(img); 
} 

void img_Click(object sender, ImageClickEventArgs e) 
{ 
    Response.Write("Hello World"); 
} 
+0

我已經做了這已經但仍然不起作用 – juhi 2010-01-06 06:40:28

+0

它爲我工作:)它應該工作。爲了什麼目的,你正在處理ImageButton? – Anuraj 2010-01-06 06:54:42

+0

但它並沒有去調用點擊方法爲什麼會這樣 有沒有在代碼中的一些問題 應該在哪裏我在Default.aspx頁面沒有提供頁面 – juhi 2010-01-08 06:23:37