473,372 Members | 1,040 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,372 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 6984
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.