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)

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

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

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

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

its fine then

thanx...

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

This question has already been answered. Start a new discussion instead.