954,577 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

raise event handler for image button

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

Function bindgrid()
{
Buttonname.click+-new ImageClickEventHandler(Buttonname_click);
}

Protected void Buttonname_click(object sender, ImageClickEventArgs e)
{
}

But it is working only when bindgrid function calls on every post back.otherwise not if bindgrid function is put under (!page.postback)

Khushi2$
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

On page load do this

//Creating Dynamic Image Button
        ImageButton objImageButton = new ImageButton();
        objImageButton.ID="dynImageButton";
        objImageButton.ImageUrl="Your Image URL";
        //Add Click event handler
        objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
        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");
}

Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 

but the problem is this function is not getting called if I am writing in Pageload the whole thing inside

]if(1Page.IspostBack)
{//Creating Dynamic Image Button
        ImageButton objImageButton = new ImageButton();
        objImageButton.ID="dynImageButton";
        objImageButton.ImageUrl="Your Image URL";
        //Add Click event handler
        objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
        form1.Controls.Add(objImageButton);       //Creating Dynamic Image Button
        ImageButton objImageButton = new ImageButton();
        objImageButton.ID="dynImageButton";
        objImageButton.ImageUrl="Your Image URL";
        //Add Click event handler
        objImageButton.Click += new System.Web.UI.ImageClickEventHandler(dynImageButton_Click);
        form1.Controls.Add(objImageButton);

}
protected void dynImageButton_Click(object sender, EventArgs e)
{
Response.Write("Inside Image Button Click");
}


Please tell me the solution of this..

Khushi2$
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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).

Aneesh_Argent
Junior Poster
104 posts since Dec 2008
Reputation Points: 16
Solved Threads: 18
 

you mean it will cant work with (!page.ispostback).

its fine then

thanx...

Khushi2$
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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 :

Protected Sub Gerak(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
MsgBox("Masuk", MsgBoxStyle.OkOnly)
End Sub


For i = 0 To jum - 1
img(i) = New ImageButton()
AddHandler img(i).Click, AddressOf Me.Gerak
img(i).ImageUrl() = "images/kodok-hijau.gif"
pGame.Controls.Add(img(i))
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...

tybalt88
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You