Groups | Blog | Home
all groups > asp.net > october 2003 >

asp.net : Problem with dropdownlist and viewstate.


robin NO[at]SPAM guavatools.com
10/25/2003 8:09:32 PM
In my user contol I am creating a set of dropdownlists.
Each list is created based on input from the other lists.
The problem I am having is setting the selected index on
the lists.

If someone changes box1, I want to set the selected index in
box2 = 0. When I do this, I dont get an error, but when the
page loads, it still has the selected value and not 0.

It seems that it is getting the selected value from the viewstate
and applying it after I set the index equal to zero. I'm setting
the index in a sub called by page.load.

I thought that at page.load the viewstate was loaded?
I tried to disable the viewstate on the drop down, but I have
not had any luck.

Any ideas?

Cowboy (Gregory A Beamer)
10/25/2003 11:21:42 PM
If you want full control, disable ViewState. You can do this for individual
components.You should be able to do this programatically, as well, and then
set the selectedIndex.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think outside the box!
**********************************************************************
[quoted text, click to view]

robin NO[at]SPAM guavatools.com
10/26/2003 10:29:19 AM
The lists are being generated in a code behind file.

Here is the code.

_optDrop = New DropDownList
_optDrop.ID = "prop" & arrayIndex
_optDrop.AutoPostBack = True
_optDrop.EnableViewState = False

you can see that I am disabling the view state.
I also disabled the view state in the control, and
the talbe that is holding it.

No luck



[quoted text, click to view]
JD
10/26/2003 1:58:34 PM
Are you dynamically adding the drop downs in the code behind? Or are they on
the ascx page?

[quoted text, click to view]

Robin Bonin
10/26/2003 2:27:21 PM
I am adding the dropdownlist into a cell that I add into a row that I add
into a table that is declared in the ascx page.
When you say after it is added to the form, are you talking about the actual
form object?
I am seting the index to 0 after it is added to the table, but I am not
doing anything with the form object itself.

If I cant get this to work I have two ideas.

This table, and drop downs and all are in a user control. Maybe I can rename
the usercontrol on each post back.
That way there would not be a view state because it is a whole new object.
The other is just to use an html select list.

but both of these are kind of messy.

[quoted text, click to view]

JD
10/26/2003 6:43:36 PM
When a control is added to a form, two important events happen in a
postback, load view state and load post back data. Load view state applies
the value that was selected when the page was rendered previously and load
post back
data will then apply the users selected value.

Now if you are creating the drop down list and setting the selected index to
0, and then adding it to the form, your value will get overwritten by the
view
state load and the postback data load. Even if you turned off viewstate,
load post back data still has to work and will overwrite your selected value
of 0. I believe if you set the selected value to 0 after you add the drop
down list to the form you should be alright.


[quoted text, click to view]

JD
10/26/2003 10:57:41 PM
[quoted text, click to view]

On the server side in ASP.NET, there is a control tree that represents the
controls on the form. Basically I'm saying when the drop down control is
inserted into that control tree, whether the parent is a form, or what you
are doing with the
form->table->row->cell->drop down. If you turn on page trace = true in your
aspx page you would see the actual control tree.

[quoted text, click to view]

Yes and you shouldn't do this, I've never had to do this. The thing with new
frameworks is figuring out how to do things "elegantly". Here is pattern I
usually follow when drawing dynamic screens:

- Draw the initial View1. Usually in the page load and not on post back.
:
- on post back I redraw View1 in the page load viewstate method, let the
viewstate and postback data logic do its thing
- handle any events from View1, these come in two flavors, first control
changed events (textboxes, drop down lists, etc..) and then post back event
(image or button submit). In these events I will do my postback event logic.
This is where you should be setting your drop down to selected = 0 because
you are responding to the specific event that determines whether to set the
drop down list selected = 0
- Draw View2 usually from the the submit event
:
- on post back I redraw View2 in the page load viewstate method, let the
viewstate and postback data logic do its thing
- handle events from View2
- Draw View3 usually from the the submit event
:
ETC...

I don't do it extactly like this but I use the model-view-controller
pattern. Using the MVC pattern allows me to split up the data, the
controlling logic and all my views. Very neat and tidy. You could probably
do a google on model-view-controller pattern and ASP.NET to find good
examples.

The page init, load viewstate and page load are for initializing either the
initial view and state, or the previous view and state of your page/control.
Not for handling your page/control events. The event handlers are where you
should handle event logic.


[quoted text, click to view]

AddThis Social Bookmark Button