raise event handler for image button

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 3
Reputation: Khushi2$ is an unknown quantity at this point 
Solved Threads: 0
Khushi2$ Khushi2$ is offline Offline
Newbie Poster

raise event handler for image button

 
0
  #1
Jan 21st, 2009
I have created image button at run time. But I want to know when I will click on this button then its click function will get call.How to create click event of this image button.

I have tried

  1. Function bindgrid()
  2. {
  3. Buttonname.click+-new ImageClickEventHandler(Buttonname_click);
  4. }
  5.  
  6. Protected void Buttonname_click(object sender, ImageClickEventArgs e)
  7. {
  8. }


But it is working only when bindgrid function calls on every post back.otherwise not if bindgrid function is put under (!page.postback)
Last edited by peter_budo; Jan 21st, 2009 at 7:51 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: raise event handler for image button

 
0
  #2
Jan 21st, 2009
On page load do this

  1. //Creating Dynamic Image Button
  2. ImageButton objImageButton = new ImageButton();
  3. objImageButton.ID="dynImageButton";
  4. objImageButton.ImageUrl="Your Image URL";
  5. //Add Click event handler
  6. objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
  7. form1.Controls.Add(objImageButton);

Your click function should be like this

protected void dynImageButton_Click(object sender, EventArgs e)
{
Response.Write("Inside Image Button Click");
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: Khushi2$ is an unknown quantity at this point 
Solved Threads: 0
Khushi2$ Khushi2$ is offline Offline
Newbie Poster

Re: raise event handler for image button

 
0
  #3
Jan 21st, 2009
but the problem is this function is not getting called if I am writing in Pageload the whole thing inside
  1. ]if(1Page.IspostBack)
  2. {//Creating Dynamic Image Button
  3. ImageButton objImageButton = new ImageButton();
  4. objImageButton.ID="dynImageButton";
  5. objImageButton.ImageUrl="Your Image URL";
  6. //Add Click event handler
  7. objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
  8. form1.Controls.Add(objImageButton); //Creating Dynamic Image Button
  9. ImageButton objImageButton = new ImageButton();
  10. objImageButton.ID="dynImageButton";
  11. objImageButton.ImageUrl="Your Image URL";
  12. //Add Click event handler
  13. objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
  14. form1.Controls.Add(objImageButton);
  15.  
  16. }
  17. protected void dynImageButton_Click(object sender, EventArgs e)
  18. {
  19. Response.Write("Inside Image Button Click");
  20. }

Please tell me the solution of this..
Last edited by peter_budo; Jan 21st, 2009 at 7:52 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: raise event handler for image button

 
0
  #4
Jan 21st, 2009
Remove the if(!Page.IspostBack) form your code which is preventing your dynamic control from recreating during postback. Let the control be created on every subsequent pageload(or postback).
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: Khushi2$ is an unknown quantity at this point 
Solved Threads: 0
Khushi2$ Khushi2$ is offline Offline
Newbie Poster

Re: raise event handler for image button

 
0
  #5
Jan 21st, 2009
you mean it will cant work with (!page.ispostback).

its fine then

thanx...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: tybalt88 is an unknown quantity at this point 
Solved Threads: 0
tybalt88 tybalt88 is offline Offline
Newbie Poster

Re: raise event handler for image button

 
0
  #6
Mar 27th, 2009
hi khushi2$...
i have same problem with u...
n i have posting in many forum n no one can resolve it... OMG...
the problem is just simple as you say...
i create imagebutton n it's click event...
but when i click the imagebutton, the event not getting called...

my code is :

  1. Protected Sub Gerak(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
  2. MsgBox("Masuk", MsgBoxStyle.OkOnly)
  3. End Sub
  4.  
  5.  
  6. For i = 0 To jum - 1
  7. img(i) = New ImageButton()
  8. AddHandler img(i).Click, AddressOf Me.Gerak
  9. img(i).ImageUrl() = "images/kodok-hijau.gif"
  10. pGame.Controls.Add(img(i))
  11. Next

when i click imagebutton, it won't show the messagebox....

maybe you can try using button... my friend tell me that she use button with the same method and work...
Last edited by peter_budo; Mar 27th, 2009 at 3:35 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC