CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2006
    Posts
    7

    Problem with Button Click Events when set during postback

    I have a dynamically created DataList control (within a composite control) that contains ImageButtons. I set button click events for these buttons during the DataBind() call (in the ItemCreated callback). This works fine on the first pass.

    However:

    When one of these buttons is clicked, I need to reconfigure the DataList (and re-assign the button click events). At this point of the page cycle the DataList has already been constructed via normal page/control initialization, and I need to rebuild it. I have used various approaches to no avail, ranging from attempting to "clear" the DataList via nulling out the datasource, re-binding it, and then rebuilding it to completely removing the composite control and reconstructing it. Regardless of how I go about this, the newly constructed data list always builds, displays, scrolls, and post back properly upon button click, but the "button click" events no longer execute (even though the events are added during the reconstruction).

    Any help is appreciated; thanks.

  2. #2
    Join Date
    Oct 2006
    Posts
    181

    Re: Problem with Button Click Events when set during postback

    You are correct dynamic controls have to be rebuilt on every page load in order to handle event or to get user input from them. PreLoad() is the recommended location to do this. However, I sometimes have to delay that to Load() because I need to know value of other controls on the page to determine what dynamic controls to generate. It seems to work fine in either location.

    You didn't mention anything about control ids. I suspect that might be your problem. You have to assign an id for every control you generate and of course they have to be unique.

    You mentioned something about reconfiguring the datalist. That has me concerned to. If you change the order controls show up you also have to make sure the id for each controls stays with it or the events will fire for the wrong control. Basically, don't use an auto number scheme like 1,2,3 etc.

    Also, make sure you don't create controls twice. If an event changes data or something and you need to recreate the list you should hold onto the controls, recreate the table, then place the controls back into the list. Otherwise, events can get messed up.

    Last, it's best to creates all the dynamic controls you could possibly need. Add them all in same order unless the order has to be different. Then make the ones don't need not visible. Also, always keep the spacing controls in the same order. For example,

    Code:
    placeHolder.Controls.Add(new LiteralControl(" "))
    Even if you change the order of spacing controls like the above things can get messed up. It has something to do with the control tree. I don't understand it but things work when I do this.

    If this doesn't help, I might be able to give a more specific answer if you posted some code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured