DropDownList Postback clears the selected value

M

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
 
L

Lloyd Sheen

MU said:
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
 
N

Nathan Sokalski

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!
 
L

Lloyd Sheen

Nathan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top