Update Panel - Controls loosing events

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StevieMars
    New Member
    • Mar 2010
    • 21

    Update Panel - Controls loosing events

    Hi All - My first post...

    I basically have an update panel being dynamically populated with a LinkButtons after a postback. This link buttons are being assigned an event. Now when I click the button the event is not firing.

    As I understand it the event is not firing because I'm not creating the button in the Page_Load event and therefore .Net does not remember this controls were created after postback.

    I'm not able to create these controls in the Page_Load as they are dependant on user input. So my question is how can I create these controls dynamically and have the page retain them and their events after postback?

    Thanks in Advance,
    Steve
  • StevieMars
    New Member
    • Mar 2010
    • 21

    #2
    found a solution although not sure if it's the best one...

    As I create the dynamic controls I also add them to an array. Then when I post back and refresh the update panel I have a method that goes through the array and puts the controls back in.

    I'm sure there is a better way to do this but I've spent way too much time googling it!!

    Any suggestions of a better way are welcome :)

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Actually you should be dynamically instantiating your LinkButtons in the Page_Init event.

      The Page_Init event occurs just before the ViewState is loaded for the page. The ViewState contains information such as Events. In otherwords, the LinkButton's event is created when the ViewState is loaded for the LinkButton. So, if the LinkButton does not exist when the ViewState is loaded the Event will also not be created....and this is why you should create your dynamic LinkButtons in the Page_Init event (which, like I said, occurs just before the ViewState is loaded).

      Check out this overview on how to use dynamic controls in asp.net.

      -Frinny

      Comment

      • StevieMars
        New Member
        • Mar 2010
        • 21

        #4
        Thanks for your reply.

        I'm generating the LinkButton dynamically in a click event of a different Button. Therefore both Page_Load and Page_Init events have already fired by this point. That's the reason why I didn't use either of these events. I'm not able to generate the LinkButtons before this as I need input from the Click Event (CommandArgumen t in case your wondering) before creating the new controls.

        Is it bad practice to create dynamic controls outside the Page_Load and Page_Init events? If so, will it not be hard to create extra dynamic controls in a click event?

        Cheers,
        Steve

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You will probably run into problems that are hard to debug if you do not declare instantiate your dynamic controls in the Page_Init event.

          If it works then just leave it....when it stops working keep this conversation in mind :)

          -Frinny

          Comment

          Working...