Question : Trying to change selection in DDL but when i read it i get the programticlly set value

Hi,
Im trying to set the index programticlly, and i have managed to do so, however when i go and submit the form i always see the value that was set by code...!

Why will it not post the value the user changed to?

here is the piece of code setting the value selected by code:
1:
2:
3:
4:
5:
6:
7:
8:
For Each item In selectCountry.Items
                    If (item.Value.ToString().ToLower() = country.ToLower()) Then
                          selectCountry.SelectedIndex = counter
                    Else
                        counter += 1
                    End If
                Next


the code im using to retrive its value is the following :
1:
2:
selectCountry.SelectedValue

and the control on the aspx itself is :
1:
2:
3:
4:
5:
<asp:DropDownList id="selectCountry" runat="server" CssClass="RequiredInput">
<asp:ListItem Value="Afghanistan">Afghanistan</asp:ListItem>
<asp:ListItem Value="Aland Islands">Aland Islands</asp:ListItem>
etc...

any ideas what im missing?
Thanks!

Answer : Trying to change selection in DDL but when i read it i get the programticlly set value

It seems your page is getting post back so your old value get restored in page load. You can check if page is getting post back then not to set value. You can do this by checking Page.IsPostBack property  in page load.
For Example
Sub Page_Load()
 If Page.IsPostBack then
     ''Do nothing
 else
   selectCountry.selectedindex = 2
 end if

end sub

Random Solutions  
 
programming4us programming4us