Dynamic Controls / Event Handlers

Posted on 2006-04-03
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?

Question by:friskyweasel
    LVL 12

    Expert Comment

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

    Author Comment

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

    Expert Comment

    Because of post back and imgbtn_ta is recreated.

    Try this,
    imgbtn_ta.Attributes.Add("onmouseover", "this.src='images/selected.gif';");
    LVL 11

    Expert Comment

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

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


    Author Comment

    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
    LVL 11

    Accepted Solution

    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;EN-US;317515

    Look how u can save the viewstates


    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now