Tutorial RSS
 
Navigator: Home - Advanced - Binding Data to DropDown, RadioButton, etc. in VB.NET

Binding Data to DropDown, RadioButton, etc. in VB.NET

In this tutorial, we will show you how to display data in form controls from a database in VB.NET. The three most common form controls that can be bound to a DataSource are DropDownList, RadioButtonList, and CheckBoxList. We will show you how to populate these controls and display the selected value(s).

When creating forms for a web application, it fairly common to use DropDownLists, RadioButtonLists, and CheckboxLists to give the visitor a few options to choose from. Occasionally the items inside of these controls are dynamic. ASP.NET makes it fairly easy to bind data from a SqlDataSource to each of these controls. For example, you may have a DropDownList that contains all of the state abbreviations so the visitor can choose from those options so they don't enter an incorrect abbreviation. This is a static example, so binding it to a datasource may be overkill. Below we will use a dynamic example.


Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!


First you can start a new Web Application in Visual Studio 2008. Now right-click on the App_Data folder and add a SQL Database. For our example we will add a Table fruit to the database with the columns Name varchar(50) and Color varchar(50). Now we can add some data to the table. Here is a visual example of our table.


Name Color
Tomato Red
Orange Orange
Apple Red
Banana Yellow
Kiwi Green

Now that we have created the table we will need to add the <asp:SqlDataSource> control to the form so each control can use it to fill itself with data. Make sure the database and table names are correct.


<asp:SqlDataSource ID="SqlDataSource_fruit" runat="server"
ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" SelectCommand="SELECT * FROM fruit" />


Next we need to add each of the controls we mentioned above. We can start by adding the <asp:DropDownList> tag from the toolbox followed by the <asp:Button> tag. Make sure the DataSourceID matches the id attribute in the <asp:SqlDataSource> tag. Now you will need to specify which column is displayed and which is the value. You can do this with the DataTextField and DataValueField attributes. In our example we will be using name as the text field and color as the value field.


Try Server Intellect for Windows Server Hosting. Quality and Quantity!


We will also need to add <asp:RadioButtonList> and <asp:CheckBoxList> tags with their corresponding <asp:Button> tags. Each of those controls will follow the same usage for the DataSource as we did with the DropDownList control. It should look like the following.


<asp:DropDownList ID="DropDownList_fruit" runat="server" DataSourceID="SqlDataSource_fruit" DataTextField="name" DataValueField="color" />
<asp:Button ID="Button_dropFruit" runat="server" Text="Submit" /><br /><br />


<asp:RadioButtonList ID="RadioButtonList_fruit" runat="server" DataSourceID="SqlDataSource_fruit" DataTextField="name" DataValueField="color" />
<asp:Button ID="Button_radioFruit" runat="server" Text="Submit" /><br /><br />


<asp:CheckBoxList ID="CheckBoxList_fruit" runat="server" DataSourceID="SqlDataSource_fruit" DataTextField="name" DataValueField="color" />
<asp:Button ID="Button_checkFruit" runat="server" Text="Submit" /><br /><br />


<asp:Literal ID="lit_status" runat="server" />


Notice the Literal control at the end of the code snippet. This will be used to display the values the user has selected when using the form.


Next we will write the code behind the aspx page. In our example we created a Click sub for each button control. This can be done by double-clicking on the button in design view. The code for the DropDownList and RadioButtonList controls will be fairly easy. The only value you will need is the selectedValue from the control to be displayed in the Literal control. Below is how it should look.


Protected Sub Button_dropFruit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_dropFruit.Click
     lit_status.Text = "The color of that fruit selected with the DropDownList is " & DropDownList_fruit.SelectedValue.ToString & "."
End Sub


Protected Sub Button_radioFruit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_radioFruit.Click
      lit_status.Text = "The color of that fruit selected with the RadioButtonList is " & RadioButtonList_fruit.SelectedValue.ToString & "."
End Sub


The CheckBoxList control is different than the other two because none or several items can be selected at one time. First, we will make a temporary string variable to store the selected values. Next we will use a For Each loop to go through each item in the CheckBoxList control. All selected values will be added to the tmp string. Once the For Each loop has ended, we will check to see if the tmp string is empty. If it is empty we will update the Literal control to tell the user that none of the items were selected. If one or more items were selected, they will be displayed in the Literal control lit_status.


Protected Sub Button_checkFruit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_checkFruit.Click


     Dim tmp As String = ""


     For Each theItem In CheckBoxList_fruit.Items
          If theItem.selected = True Then
               tmp += theItem.value & ", "
          End If
     Next


     If String.IsNullOrEmpty(tmp) = True Then
          lit_status.Text = "No fruit was selected with the CheckBoxList."
     Else
          tmp = tmp.Substring(0, tmp.Length - 2)
          lit_status.Text = "The color of that fruit(s) selected with the CheckBoxList are " & tmp & "."
     End If


End Sub


Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


Now when you select an item and press submit the Literal control will update with the value(s) selected and from which control it was selected with. We used a SqlDataSource but other DataSources such as ArrayList or Hash Tables can be used to fill the controls we used with content.



Looking for more ASP.NET Database Tutorials? Click Here!


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

411asp.net123aspxDotNetFreaksServer Intellect