Michael Falconer

the difficult takes time, the impossible just a little longer

My Links

Blog Stats

News

Michael Falconer is a freelance/contract .net developer working in and around Glasgow, Scotland, mainly on asp.net applications. His company is called, for some strange reason, Camel-Jones.

Archives

Post Categories

Blogs

Other Links

Regulars

Handling postbacks in composite web controls

I've been developing a composite web control lately to provide numeric paging for a web form. One problem I hit was how to change the structure of the child controls after handling a postback event. The typical order of events for a composite control is:

Load -> CreateChildControls -> Handle Postback -> Render

I was dynamically creating some of the child controls based on fields that could be modified by the Postback events. The problem was, as you can see from the order above, the child controls are created before the fields are set.

Thankfully I found this post on .NET 247 by Arthur Mnev. He had exactly the same problem and had found a neat solution. If you set the controls ChildControlsCerated flag to False in the event handler, the .net framework will then regenerate the child controls after the event handler has completed. It does incur a penalty as the CreateChildControls method is run twice, but it works very nicely. As he points out you do have to make sure the that the controls are created is the correct order the first tmie the code runs, but in most cases this will not be an issue.

To get round the double running of the code, he does mention that he 'changed it to redirect itself to its own page with Get Parameters instead of events'. Something else for me to take a look at soon...!

posted on Monday, June 07, 2004 12:20 PM