472,096 Members | 1,186 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Problem with dropdownlist and viewstate.

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?

Thanks
Nov 17 '05 #1
6 6852
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!
************************************************** ********************
"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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?

Thanks

Nov 17 '05 #2
JD
Are you dynamically adding the drop downs in the code behind? Or are they on
the ascx page?

"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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?

Thanks

Nov 17 '05 #3
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

"JD" <no@where.com> wrote in message news:<eqQmb.23707$mZ5.91684@attbi_s54>...
Are you dynamically adding the drop downs in the code behind? Or are they on
the ascx page?

"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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?

Thanks

Nov 17 '05 #4
JD
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.
"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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

"JD" <no@where.com> wrote in message

news:<eqQmb.23707$mZ5.91684@attbi_s54>...
Are you dynamically adding the drop downs in the code behind? Or are they on the ascx page?

"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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?

Thanks

Nov 17 '05 #5
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.

"JD" <no@where.com> wrote in message
news:rBUmb.24735$mZ5.101355@attbi_s54...
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.
"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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

"JD" <no@where.com> wrote in message

news:<eqQmb.23707$mZ5.91684@attbi_s54>...
Are you dynamically adding the drop downs in the code behind? Or are they on the ascx page?

"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
> 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?
>
> Thanks


Nov 17 '05 #6
JD
> 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?
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.
but both of these are kind of messy.
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.
"Robin Bonin" <ro***@guavatools.com> wrote in message
news:l8Wmb.31050$iq3.22551@okepread01... 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.

"JD" <no@where.com> wrote in message
news:rBUmb.24735$mZ5.101355@attbi_s54...
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.
"Robin Bonin" <ro***@guavatools.com> wrote in message
news:f1**************************@posting.google.c om...
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

"JD" <no@where.com> wrote in message

news:<eqQmb.23707$mZ5.91684@attbi_s54>...
> Are you dynamically adding the drop downs in the code behind? Or are

they on
> the ascx page?
>
> "Robin Bonin" <ro***@guavatools.com> wrote in message
> news:f1**************************@posting.google.c om...
> > 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?
> >
> > Thanks



Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kalyani | last post: by
3 posts views Thread by Bas Paap | last post: by
5 posts views Thread by AlexC | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.