<

Getting and Setting Value in Bound DropDownlist

Published on
10,146 Points
3,646 Views
Last Modified:
Approved
Community Pick
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
Comment
Author:AmmarR
0 Comments

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month