473,500 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList Postback clears the selected value

MU
Hello

I have a form with a dropdownlist on it and in the page_load I have
this to populate it:
Dim productsAdapter As New ProductsTableAdapter
drpProducts.DataSource = productsAdapter.GetProducts()
drpProducts.DataTextField = "Title"
drpProducts.DataValueField = "ProductID"
drpProducts.DataBind()

Here is the drop down list:
<asp:DropDownList ID="drpProducts" runat="server"
AutoPostBack="True" class="textbox">
</asp:DropDownList>

It populates with the correct fields etc but when the postback
happens, it refreshes the list and goes back to the 1st item in the
list, it doesn't maintain view state. IE. if I choose the 3rd in the
list, it doesn't refresh the page back and select the 3rd in the list
again, I'm assuming it's because of the code in the page_load.

How do I make it stay on the selected list item as I will be using it
with a gridview below it to edit records.

Thanks
MU
Oct 23 '08 #1
3 12840
MU wrote:
Hello

I have a form with a dropdownlist on it and in the page_load I have
this to populate it:
Dim productsAdapter As New ProductsTableAdapter
drpProducts.DataSource = productsAdapter.GetProducts()
drpProducts.DataTextField = "Title"
drpProducts.DataValueField = "ProductID"
drpProducts.DataBind()

Here is the drop down list:
<asp:DropDownList ID="drpProducts" runat="server"
AutoPostBack="True" class="textbox">
</asp:DropDownList>

It populates with the correct fields etc but when the postback
happens, it refreshes the list and goes back to the 1st item in the
list, it doesn't maintain view state. IE. if I choose the 3rd in the
list, it doesn't refresh the page back and select the 3rd in the list
again, I'm assuming it's because of the code in the page_load.

How do I make it stay on the selected list item as I will be using it
with a gridview below it to edit records.

Thanks
MU
I should say see the above post and reply but you need to put an
IsPostBack in your form load or you will overwrite the state of the
postback every time.

LS
Oct 23 '08 #2
I sometimes have trouble with this as well, but I think it is usually better
to do the populating in the Page_Init event. ViewState is not loaded during
the Init event, so you won't be overwriting it. I make no promises, since I
am not an expert on ViewState/PostBack problems, but see if this makes any
difference. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"MU" <mi*****@lonelyprogrammer.comwrote in message
news:c2**********************************@m32g2000 hsf.googlegroups.com...
Hello

I have a form with a dropdownlist on it and in the page_load I have
this to populate it:
Dim productsAdapter As New ProductsTableAdapter
drpProducts.DataSource = productsAdapter.GetProducts()
drpProducts.DataTextField = "Title"
drpProducts.DataValueField = "ProductID"
drpProducts.DataBind()

Here is the drop down list:
<asp:DropDownList ID="drpProducts" runat="server"
AutoPostBack="True" class="textbox">
</asp:DropDownList>

It populates with the correct fields etc but when the postback
happens, it refreshes the list and goes back to the 1st item in the
list, it doesn't maintain view state. IE. if I choose the 3rd in the
list, it doesn't refresh the page back and select the 3rd in the list
again, I'm assuming it's because of the code in the page_load.

How do I make it stay on the selected list item as I will be using it
with a gridview below it to edit records.

Thanks
MU

Oct 24 '08 #3
Nathan Sokalski wrote:
I sometimes have trouble with this as well, but I think it is usually better
to do the populating in the Page_Init event. ViewState is not loaded during
the Init event, so you won't be overwriting it. I make no promises, since I
am not an expert on ViewState/PostBack problems, but see if this makes any
difference. Good Luck!
Nathan you only want to populate the form once. That is when the form
is loaded (the first time). The way to test is the IsPostBack. You do
not need to load twice.

LS
Oct 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6990
by: Robin Bonin | last post by:
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...
5
1662
by: tshad | last post by:
Here is the DropDown: <asp:DropDownList ID="ScreenTest" runat="server" /> Here is the code: ScreenTest.DataSource=objCmd.ExecuteReader ScreenTest.DataValueField="ScreenTemplateMasterID"...
10
6345
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
4
6113
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
3
10557
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the...
18
5405
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
1
4635
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
3465
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
4905
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.