We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Dynamic Controls / Event Handlers

friskyweasel asked
Medium Priority
Last Modified: 2012-08-13
Hi all - I've found a couple of other posts on this topic, but I'd like to get a solution to my specific situation. I'm dynamically building a table, and for each row in the table i'm adding an image button and several textboxes dynamically. The imagebuttons post back when clicked, but I have some other code that changes the ImageURL property of the image button. This code is in the onlclick event, but it appears this event isn't firing for the imagebuttons. The textboxes seem to be keeping their state just fine as far as i can tell...they all "come back" after i've posted to the page, and they all retain their values...

I have something like this in my client side page:
<TABLE id="MainTable" border="0" runat="server"></TABLE>

then in my code behind page, I call something like "AddRow1();" from inside the Page_Load event...inside AddRow1 is something like this for adding image buttons:
//add image button column
HtmlTableCell imgbtn_cell = new HtmlTableCell();
imgbtn_cell.NoWrap = true;
ImageButton imgbtn_ta = new ImageButton();
imgbtn_ta.ImageUrl      = "images/edit.gif";
imgbtn_ta.ID = "imgbtn_ta";
//imgbtn_ta.Click += System.Web.UI.ImageClickEventHandler(imgbtn_ta_Click());

I have a hard-coded version of the page(all controls manually added),  which has this event handler for the image button - it changes the ImageURL once the image button is clicked, to show that it is now the currently selected row of the table:
private void imgbtn_ta_Click(object sender, System.Web.UI.ImageClickEventArgs e)
      imgbtn_ta.ImageUrl = "images/selected.gif";

I tried just bringing this event handler over to the dynamic page, but that didn't work out too well at all : ). Can someone give me the quickest, cleanest way to get my event handlers working?

Watch Question

simply use this,
        imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';");


I put that change in but still getting the same behavior...clicking the image buttons posts the form, but the image associated with the button doesn't change...

Because of post back and imgbtn_ta is recreated.

Try this,
imgbtn_ta.Attributes.Add("onmouseover", "this.src='images/selected.gif';");
 imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';return false;");

use the return false statement in end. That stops the post back.



hi guys - thanks for the responses -  I think maybe I wasn't entirely clear about what I wanted.

I'm not trying to bypass or disable the postback - I actually want to keep the postback in there...just as it was when i built the form manually by dragging the ImageButton onto the form using the visual designer - What I want that I can't quite get to work is to be able to execute some code in the OnClick event of that Image button...but I'm missing something somewhere because even though it posts back to the page when i click the image button, it seems to be completely ignoring the OnClick event handler
Accepted Answer from TheAvenger
Date: 01/06/2005 04:55PM TPT
Grade: A
 Accepted Answer  

It is important that once you create a dynamic control and then post the page back, you have to recreate the dynamic controls. This means: When you click the button, the page is sent again to the server. Now your application should know that a control was already created (on the previous step) and recreate it in the Page_Load. Then it will handle the new click.

In general: whenever new controls are created, you need to recreate them once the page is posted back.

See the following pages for more details:

By default, dynamically created controls are not saved to the Page's ViewState, meaning that they are lost across postbacks. So every time you get back to form (post back your form ) you need to recreate them.

look how to create dynamic controls and use them http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515

Look how u can save the viewstates


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


good articles - that works for me - for the benefit of others out there, this is what I did in a nutshell that seems to work:

used something like this to dynamically create my control - in this case an image button - Important -I had to put this code in the "OnInit" section of the code behind page...otherwise it didn't keep the state of my control values properly...and also caused problems with firing off my event handlers for the image buttons..

HtmlTableCell mybutton_cell = new HtmlTableCell();
ImageButton mybutton = new ImageButton();
mybutton.ImageUrl = "images/someimage.gif";
mybutton.ID = "mybutton";
mybutton.Click += new System.Web.UI.ImageClickEventHandler(this.mybutton_Click);

then I just manually added an event handler somewhere out in my main page code:
private void mybutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
//do whatever processing you need here...

the key seems to have been placing the dynamic control declaration code in the "oninit" section - maybe this will help somebody else with a similar problem...thanks jatinderalagh for digging up that great post
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.