I have a custom control which has a RadioButtonList control in it. On selecting one of the radio buttons , the control does a postback to do some serverside work and the page reloads with the correct radiobutton selected. This works fine in my test page.
I've now moved the control into a page on the site it's going to live in and when the radiobutton is selected, the postback occurs but when the page reloads the radiobutton is no longer selected. So it appears the the selection is no longer being saved into viewstate.
Any pointers as to what aspects of the new page I should be looking at would be very gratefully received.
Cheers
Adendum - a bit more info
Thanks for the advice Stephen
The control was originally developed as a web user control using the designer, very simple implementation , server side handlers for onload and when the radiobutton selection changes. Separate dll for code behind and an ascx. I then converted it to a custom control to pull the ascx into the dll with the compiled code behind.
I don't dynamically create the control in the test page, there's a tag and a <%@ Register ...blah blah> at the top in the aspx to do that. The radiobuttonlist was dragged and dropped in the ascx using the designer, so I didn't implement the code to create it. So all very vanilla. I'm really looking for the differences that I should be looking at in the pages that use the control, to work out why it works in the test page but not in the site page it needs to reside in.
*********** Addition *********** I had a short time to solve this problem and had to hand it over to another developer. When I find out from him what the solution was I'll post it here. Many thanks to all for their input.
*********** Answer ************* Here's the answer from the developer that took over.
Your quiz control was fine mate, the reason the values weren't being retained on postback was because of inherited functionality in the base class when you put it live. Essentially it did a DataBind in the page load method w开发者_如何学Pythonhich in the page lifecycle is before the postback meethods get called, so it reset the values of the dropdowns. In the end I set up some viewstate properties which held the values of the dropdowns. So it wasn't something you could have known about.
Make sure your page has
EnableViewState=True
First off, this is NOT a ViewState issue. Viewstate is for programmatic changes to the controls on a page. If you set the text of a label to something different than what resides on the ASPX in CODE BEHIND then that value is stored in viewstate. .NET has a mechanism (and it's controlled via an interface that your control needs to implement) which parses out the POST data back to the controls where it belongs which occurs after ViewState has been loaded but before the Page_Load event fires.
Now, there's a couple different possibilities for the reasons behind this:
First, is this radiobutton inside your custom control being generated inside the CreateControls method or is it straight HTML that your control is rendering? If it is straight HTML then you'll need to ensure that you're handling the expected values as they come back from postback. If it is a server control being declared as NEW inside your custom control and not being instantiated and dealt with in the CreateControls method then you need to mvoe it there.
Second is this control on the ASPX page or is it being added to the page in the code behind. If it is being added to the page in code behind it's probable that it's being added after the .NET parser has finished handing out the postback data. Check your forms collection to see if the value exists there.
Last time I needed ViewState for a Radio Button I had to override the LoadViewState and SaveViewState methods to get it to work correctly.
Got a response from the developer that took over the project ->
"Your quiz control was fine mate, the reason the values weren't being retained on postback was because of inherited functionality in the base class when you put it live. Essentially it did a DataBind in the page load method which in the page lifecycle is before the postback meethods get called, so it reset the values of the dropdowns. In the end I set up some viewstate properties which held the values of the dropdowns. So it wasn't something you could have known about."
Hope this helps someone.
精彩评论