Last post Jan 20, 2012 10:26 AM by dean s
Jan 17, 2012 11:58 PM|dean s|LINK
I have a templated databound composite control that creates child controls specifally HtmlAnchor and HtmlGenericControl("span"). The click event of the child control fires after CreateChildControls which makes sense that .net has to reassemble the controls
and apply view state and events and stuff. But the problem I have is when that link is clicked I want to dynamically create links with server events. BUt at this point the child heirarchy has been rendered. I tried the things below but only way I think I can
get it to work is by calling Databind from the event and storing my DataSource in ViewState which I don't want to do. Tomorrow I am going to try tweaking it to inherit from CompositeControl but I dont think that will help. I am using the pattern from this
Scott Mitchel article.
What I've tried
I tried to add them at Render which did render them out correctly but there click events would not fire. Even thought the markup has __doPostback in it.
I tried calling CreateChildControls which calls CreateControlHeirarchy(false) again but then the spans innerText and image were gone - I assume I somehow wipedd out ViewState doing this?
Jan 19, 2012 08:02 PM|Decker Dong - MSFT|LINK
I am using the pattern from this Scott Mitchel article.
This means that you are now using a databind to bind all the things to the data presentation controls like GridView or Repeater……This will be OK。
Please make sure that you've added a ChildControlsCreated=True at the back of "CreateChildControls" event。
I think I can get it to work is by calling Databind from the event and storing my DataSource in ViewState which I don't want to do.
Why？You should know that HTML is non-memory，this means it cannot remember what you did in the previous time and what's the next。So you should have a technology like ViewState or something to remember it……For a complicated situation，you can override LoadViewState
and SaveViewState to define your own method。
Jan 20, 2012 10:26 AM|dean s|LINK
Thanks for responding. I just wanted to make sure I was following a proven recomended pattern. If its good enough for MSFT its good enough for me. I did implement it by recalling Databind from inside the contrl and addding the Datasource to view state. My
concerns for view state for strictly for keeping the page lean and mean. I do realize the web is stateless, its just the tutorials out there don't Add and retrieve from ViewState in the property getter and setter (though I imagine it is still in viewstate
or something under the covers since I create a dummy object and it magically gets populated.
Only thing I don't like it create controls being called twice in the post back but oh well the control is still real fast and not doing anything intensive in the method. Thanks for you help!