473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 6978
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
6
by: hb | last post by:
Hi, I have a page bill.aspx and its code-behind bill.aspx.cs. On bill.aspx I have: === Select a month: <asp:dropdownlist runat="server" id="lstDate" autopostback="True" /> <br> <asp:table...
1
by: Kalyani | last post by:
Hi, I have a page with one DropDownList and a Placeholder.This placeholder wil contain either a textbox or a DropDownList depending upon the value in previous DropDownList. Also there is a Submit...
3
by: Bas Paap | last post by:
I'm having a problem with formviews and DropDownLists in ASP.NET 2.0. I'm using a formview to insert an order into a database. Part of the order is the customer. Currently, I have a dropdownlist...
7
by: AG | last post by:
Hi, ASP.NET 2.0 web app I have a GridView utilizing template columns and bound to an objectdatasource. In the edit template of one column there is a dropdownlist bound to another...
5
by: AlexC | last post by:
Hi, i have just read some threads about viewstate and wanted to test myself. But there must be something i don't undertstand, because when i submit the form with EnableViewState="false", i get...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
6
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.