Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UserControls ViewState Problems

Status
Not open for further replies.

Modica82

Technical User
Jan 31, 2003
410
GB
Hi all,

i have problems with user controls which are dynamically placed on my page. i have a user control that houses two drop down lists, these two drop down lists are to be linked, however, the viewState of the first drop down list never seems to be maintained, i have read somewhere that this is due to the loading mechanism of the page (pageLoad(), onInit() etc. My question is how can i get my User Control to maintain its state on the page??

Please Help!

Rob
 
Does anyone have any ideas of how i can maintain the state of a user control????

Anyone.....

Rob
 
Rob,

You are correct in that it COULD be the culprit. I ran into the exact problem when I developed a page to look up ATM's.

The first dropdown listed states. When the user selected a state, it posts back and displays the second dropdown with cities in the selected state. When selecting the city displays a repeater control with ATM's from that city.

Anyhow, I had problems getting the state dropdown to maintain their selection. And here is the code in the code-behind page in the Page_Load event:

If Not IsPostBack Then

'Call sub that loads the state dropdown list.
LoadStateDropDownList()

'Hide the dropdownlist for the cities.
drpCityList.Visible = ("False")
Else
'Page is posting. Call sub that loads the city for the selected state
If Not drpCityList.Visible Then
LoadCitiesForSelectedState()
End If

End If

HTH,
Mark
 
The problem with the dynamic controls is that when the page posts back it looks at your aspx file. Since the dynamically generated controls are obviously not in that file the server doesn't know about them or create them.


John has the right idea on this.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I understand what you are saying and that was my initial plan however i cant really do that as i have designed my system in such a way that it needs the injection of dynamic modules as the page categories change on such a regular basis that i found this would be the best solution as apposed to creating a new page for each section.

I know this can be done, i guess more research is needed, however if anyone has any ideas then please send them my way!

Rob
 
Ok somehow i have managed to fix something, but now have another problem. I changed the page_load event in the user control to page_init, and now it seems to retain the state of the dropdown list when the page is posted back, but my problem is now is the population of the first drop down.

if i have the following code it works:

sub Page_init(ByVal Sender As Object, ByVal e As EventArgs)

BindData()

End Sub

Obviously it returns back to the database and returns the datareader i use to poplate the dropdownlist, but whats interesting is it maintains the state of the item selected, for instance if i select Italy from the list then that is what is there when the page is refereshed.

However if i have:

If Not IsPostBack Then
BindData()
End If

withing the onInit event my first drop down does not get populated????? this is really getting to me know, can anyone give me some ideas please.

Rob
 
could you use session variables to hold the values? or hold the values in an array list and save the array to a session variable?

Jason Meckley
Database Analyst
WITF
 
Please excuse the last post I am wrong I was thinking custom controls not user controls.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top