create image button dynamically

I am trying to create image control  button's dynamically. I am able to get the image  button's to display, but the on clientClientClick function I wrote is not capturing the event when I click on the button. Am I missing something? Please see code below. Thanks.

            Table table = (Table)this.FindControl("_table");
            TableRow tr = new TableRow();
            TableCell tdButton = new TableCell();

             ImageButton button = new ImageButton();
             button.ID = "imgButton_" + i;
             button.OnClientClick = "imgButtonClick";
             button.ImageUrl = "images/folder.gif";
             button.Visible = true;



Who is Participating?
Hamed ZaghaghiConnect With a Mentor ProgrammerCommented:
i use this in 2

        ImageButton button = new ImageButton();
        button.Click += new ImageClickEventHandler(button_Click);

and implement the button_click like this
    protected void Button1_Click(object sender, EventArgs e)
        TextBox1.Text = DateTime.Now.ToString();
Carl TawnSystems and Integration DeveloperCommented:
Is "imgButtonClick" a javascript function ?
Hamed ZaghaghiProgrammerCommented:
when you use OnClientClick porperty of an image button, it's means that you have a script function named imgButtonClick in your aspx page and use that function for client click,

but if you want to handle image click event on the server you must write code like this:

button.OnClick += new EventHandler(imgButtonClick);
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

LeeHenryAuthor Commented:

I tried it out, but i'm getting the below error:

Error      2      'System.Web.UI.WebControls.ImageButton.OnClick(System.Web.UI.ImageClickEventArgs)' is inaccessible due to its protection level      
Hamed ZaghaghiProgrammerCommented:
Ooooooops error in paste

and implement the button_click like this
    void button_Click(object sender, ImageClickEventArgs e)
Hamed ZaghaghiProgrammerCommented:
also you can do this:

first declare the image button
ImageButton button = new ImageButton();

secon use visual studio features to create the event handler:


button.Click +=

then press TAB to create this:

button.Click += ImageClickEventHandler(button_Click);

and then press TAB again to create the button_click method

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.