Community Pick: Many members of our community have endorsed this article.

Getting and Setting Value in Bound DropDownlist

Published:
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method.

So in this article I will demonstrate how to add and use the dropdownlist. Three basic steps, first is to add the dropdownlist, then we get the values for the list, and then the last step is to Set the value selected.


Step1 : Add a dropdownlist

1. open up your aspx page
2. Go to Toolbox and under the Standard Tab, you will find the DropDownList control
3. Double click on it (it will appear on the page)
4. move it to the location you desire on the page
5. click on the small right arrow of the dropdownlist and chose (Chose Data source)
6. follow up with the wizard and decide the value & Displayed Text of the dropdownlist


step2 : Get the value

To get the value its a very simple and straight forward, all you have to do is call the SelectedValue property. In this example I will add a button to the page just for the sake of the demo.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                              Dim var1 As String
                              var1 = DropDownList1.SelectedValue
                          End Sub

Open in new window

     


Step3 : Set the value selected

Here comes the tricky part.

If you try the old (Logical) way of setting the value (like the code below), the basic approach would be, getting the value from a database and you keep it in a variable (var1) and now you want to set the selected value of a dropdownlist to var1.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                              Dim var1 As String
                              var1 = "News" ' a value from the database 
                              DropDownList1.SelectedValue = var1
                          End Sub

Open in new window


But, trying that approach, you get the following error:

'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

This is because the Dropdownlist box has not been bound to the database yet !

So before setting the selectedvalue, you need to do the following :

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                              Dim var1 As String
                              var1 = "News" ' a value from the database 
                      		DropDownList1.DataBind() ' bind the dropwdownlist to the database and get it filled
                              DropDownList1.SelectedValue = var1
                          End Sub

Open in new window


After adding this code, the error will disappear, but a new problem, the selectedvalue wont work.

That is because its a readonly property. I dont know why, but neither the Selectedvalue nor the SelectedItem of the dropdownlist are settable, both are readonly
and the only property that you can use to set the selected item is the SelectedIndex. But in our case here, we have stored the SelectedValue in our variable and we want to use that...

So to get it to work, you need to use the following code :

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                              Dim var1 As String
                              var1 = "News" ' a value from the database 
                              DropDownList1.DataBind()
                              DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(var1.ToString.ToLower.Trim))
                          End Sub

Open in new window


You also need to make sure that the value in the dropdownlist is lowercase & trimmed as well, because the FindByvalue function is case sensitive. In the Html part of the page where the dropdownlist is directly retrieving datafrom the database, make sure you get the value in lowercase, see code below :

    <asp:DropDownList ID="DropDownList1" runat="server" 
                              DataSourceID="SqlDataSource1" DataTextField="Value" DataValueField="ID">
                          </asp:DropDownList>
                          <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                              ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>" 
                              SelectCommand="SELECT  rtrim(ltrim(lower([Value]))), [Title] FROM [MyItems]"></asp:SqlDataSource>

Open in new window



By making sure you understand and follow of all the above steps, it will also work for you. Happy coding.

Regards
0
4,197 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.