<

[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x

Getting and Setting Value in Bound DropDownlist

Published on
9,997 Points
3,497 Views
Last Modified:
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Other articles by this author
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month