how do I use viewstate to get my value back after postback?

C

Calvin X

Hi all I have the following code in the page load event. The function in
this code works get to populate the 4 dropdownlists on my webform. I have
enabled postback because when the user selects from one of the boxes it
should filter the other dropdownlists according to the data selected in the
one causing the postback. However, when the post back runs the drop down
boxes are empty and there are no values in the list. I realize that this is
because I have not reloaded the data into them, my question of the day is:

How dow I set a value into the viewstate before the postback is fired and
then how do I get it back after the postback has started?

If Page.IsPostBack = False Then
CreateComboDataSourceA()
CreateComboDataSourceB()
CreateComboDataSourceC()
CreateComboDataSourceD()
End If


Thanks,

Calvin X
 
G

Guest

you should have a look at the EnsureChildControls of the page. you need to
say something like,

If Not Page.IsPostBack Then
' Do Blah BLah Blah
Else
EnsureChildControls()
' In post back, drop downs should now be available.
' Do Blah BLah Blah
End If
 
K

Karl

You guys are asking tough questions today.

If the choices in dropdownlistB are bound to the selection of A, and C bound
to B and D bound to C, you should be using the selectedIndexChange event of
each box to populate the next one.

I can't say I understand why your dropdownlists are losing their values on
postback. If you don't have EnableViewState="false" anywhere, I'd expect
your values to be maintained. So that I'm clear, it sounds to me like you
are simply saying that DropDownListA has a bunch of options, when one is
selected it postsback, at which point DropDownListA has no values?

Karl
 
C

Calvin X

I want the lists to fill will all the values. If the user selects an item in
dropdownboxA then the other three will be altered via the
SelectedIndex_Changed event, which is not firing because there is no value
in the dropdown controls.

So that I'm clear, it sounds to me like you
are simply saying that DropDownListA has a bunch of options, when one is
selected it postsback, at which point DropDownListA has no values?

Karl
YES Exactly.

Calvin X
 
G

Guest

as for the viewstate part,

you set a value by doing ViewState["key"] = value;
you read it by doing value = ViewState["key"];
 
C

Calvin X

but how can I put my selected value into the viewstate before the postback?

chriscollinson said:
as for the viewstate part,

you set a value by doing ViewState["key"] = value;
you read it by doing value = ViewState["key"];

Calvin X said:
Hi all I have the following code in the page load event. The function in
this code works get to populate the 4 dropdownlists on my webform. I have
enabled postback because when the user selects from one of the boxes it
should filter the other dropdownlists according to the data selected in the
one causing the postback. However, when the post back runs the drop down
boxes are empty and there are no values in the list. I realize that this is
because I have not reloaded the data into them, my question of the day is:

How dow I set a value into the viewstate before the postback is fired and
then how do I get it back after the postback has started?

If Page.IsPostBack = False Then
CreateComboDataSourceA()
CreateComboDataSourceB()
CreateComboDataSourceC()
CreateComboDataSourceD()
End If


Thanks,

Calvin X
 
C

Calvin X

The SelectedIOndex_changed event never fires when I select a value...it just
goes blank.
 
C

Calvin X

ok I sent you my code...

thanks for your help with this. i tried to run the code from an earlier
problem I found in the forums similar to this but the selectedindex changed
code still never ran in that project either. I think the code was from the
"cannot fire up dropdownlist's SelectedIndexChanged" thread

Thanks again
 
C

Calvin X

Okay, I think I have nailed down the problem. When I add items directly
using the designer than the slected index changed event fires and the
postback keeps the selected value. Now the problem is ho do I get my
dynamic lists to load and still make this work. Should I write some inline
ASP.NET that prints out the values? I will try that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top