DropDownList Postback clears the selected value

  • Thread starter Thread starter MU
  • Start date Start date
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
 
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
 
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 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
 
Back
Top