473,503 Members | 2,029 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 12842
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
6992
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
1663
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
6346
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
6116
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
10560
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
5409
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
1
4637
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
3466
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
4908
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
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
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...
0
7464
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
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
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
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
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.