Composite Control - Not retaining Value (fully) when page posts back.

I've just got put back onto my old project and I spent a few hours getting back to grips with it.  The main part of it is a Composite Control I've built.

This composite control builds itself from an XML file. It might contain 10 drop downs, 5 textboxes, or even 100 radio button lists.

I have a strange issue when the parent page is refreshed via. a PostBack.

 ----

I set a drop down's value to the 9th item in the list. I click a button on the page to refresh it via a PostBack.


When I go through the :         public override void RenderControl(System.Web.UI.HtmlTextWriter writer)  event,  I will write out a DropDown list like so:

  

switch (((string[])((Control)f.Control).ID.Split('_'))[0])
                            {
                                case "DropDown":
                                    using (DropDownList ddl = ((System.Web.UI.WebControls.DropDownList)f.Control))
                                    {
                                        FieldManagement.FieldList fl = (FieldManagement.FieldList)o;
                                        ddl.DataSource = fl.FieldListValues;
                                        ddl.DataTextField = "Text";
                                        ddl.DataValueField = "Value";
                                        ddl.DataBind();
                                        ddl.RenderControl(writer);
                                    }
                                    break;
 

 When that drop down list is rendered, it might have the selected Value of 8. I can put the control in the watch and the value will be 8.  However, when it gets rendered to the screen, it will go back to the default (0).

It does this for ALLL controls, except the TextBox - it gets rendered correctly. 

Any thoughts? 

 I haven't change my code at all, and it used to work, and now it doesn't so I'm trying to get my mind back in this mindeset.


Thanks.
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
4/11/2007 12:41:12 AM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

6 Replies
694 Views

Similar Articles

[PageSpeed] 18

databinding a ddl will clear any selectedvalue.

it appears you are running databind every time you render.

 


Mike Banavige
~~~~~~~~~~~~
Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/11/2007 1:33:00 AM

True, I figured that out as soon as I posted this :D

 However, I still need to rebind the DLL's inside the composite control on each postback otherwise they won't have their values correct?   
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
4/11/2007 2:47:39 PM

if the control exists on your page and viewstate is enabled, the ddl will remain populated and the selected value should be correct

if the control was dynamically added to your page (w/viewstate enabled), as long as it is added by page_load at the latest, the ddl will remain populated with its selection.

for other scenarios, you could databind the ddl to repopulate it and then read the ddl's clientid from the post data (like classic asp) and then re-select the previously posted value.

 


Mike Banavige
~~~~~~~~~~~~
Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/13/2007 1:52:05 AM

Thanks for the info.

 The 3rd option you mentioned doesn't seem at all efficient, and I do retain the values in ViewState.  Thanks for the help!  
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
4/13/2007 2:57:06 PM

So your problem is solved?
Did you simply remove the databind on postbacks?

and yes - the 3rd option is usually not the preferred option - unless viewstate cannot be used.

 


Mike Banavige
~~~~~~~~~~~~
Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/14/2007 1:43:53 AM

yes it is, thank you for reminding me to mark yours post as an answer.

 Viewstate is a bit iffy with the way my control is created and how it is loaded. Though I don't use AJAX inside my control, the parent page will use ajax around it which sometimes messes with ViewState... especially seeing I dynamically create things like Cascading Drop Downs (Ajax control toolkit control) inside the composite control.

 

Thanks for the help!   
 


The Killer Ninja Coding Monkeys thank those that mark helpful posts as answers.

My Site | My Examples | My Blog


0
RTernier
4/14/2007 3:40:09 AM
Reply: