Creating a Dropdown List in Visual Basic.NET
In this tutorial we will show you how to create a dropdown list and how to add to it with user input. Having a dropdown list on a website is a great tool because it can save space and make the site look much cleaner and professional. For example, instead of having multiple links on a page, you can have a dropdown list that leads to links.
1. Open Microsoft Visual Studio.
2. Click File > New > Web Site.
3. Right click on the solution name in Solution Explorer and click Add New Item. Make sure Visual Basic is selected as the installed template. Select Web Form and leave the name at Default.aspx.
4. Inside the <div> tags enter new paragraphs tags by writing <p> </p>.
5. Inside the <p> tags click, drag and drop a TextBox control in from the toolbox between those tags. After the closing textbox tag insert a break line by writing <br />.
6. After the break tag, click drag and drop a Button from the toolbox. After the closing button tag insert a break line tag (<br />). In the Text field of Button, change the text from Button to Add.
7. After the break line tag click, drag and drop a DropDownList from the toolbox.
8. In between the opening and closing DropDownList tags we are going to add items to the dropdown list. We do this by writing <asp:ListItem Text=””></asp:ListItem>., with the word in the quotations of Text being the item on the list.
9. While in Default.aspx, switch to Design view. Double click the button Add.
This takes us to Default.aspx.vb, the Visual Basic coding of the page. You should see a Button1_Click method. Inside that method we need to tell it to add what the user wrote in the text box into the dropdown list.
10. Inside the Button1_Click method, write DropDownList1.Items.Add (TextBox1.Text).
This line of code gets access to items in DropDownList1 and adds what is written in TextBox1 through the Add method.
Run the application and click on the dropdown list. There will be two items, Shenmue and Sonic Adventure. Now write anything in the text box and click Add. Now open the dropdown list again and you should see what you wrote on the list. Keep in mind this tutorial does not show how to validate data in the text box and duplicates in the dropdown list, so that will occur. Just grasp the basics of setting and adding to a dropdown list.
WebSite8.zip
1. Open Microsoft Visual Studio.
2. Click File > New > Web Site.
3. Right click on the solution name in Solution Explorer and click Add New Item. Make sure Visual Basic is selected as the installed template. Select Web Form and leave the name at Default.aspx.
4. Inside the <div> tags enter new paragraphs tags by writing <p> </p>.
5. Inside the <p> tags click, drag and drop a TextBox control in from the toolbox between those tags. After the closing textbox tag insert a break line by writing <br />.
6. After the break tag, click drag and drop a Button from the toolbox. After the closing button tag insert a break line tag (<br />). In the Text field of Button, change the text from Button to Add.
7. After the break line tag click, drag and drop a DropDownList from the toolbox.
8. In between the opening and closing DropDownList tags we are going to add items to the dropdown list. We do this by writing <asp:ListItem Text=””></asp:ListItem>., with the word in the quotations of Text being the item on the list.
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Code Block
Default.aspx
Creating a textbox, button and dropdown list is as simple as clicking and dragging the tool from the toolbox into Default.aspx. Adding an item to a dropdown list is done with <asp:ListItem> with the text being added to the dropdown list.<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Add" />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Shenmue"></asp:ListItem>
<asp:ListItem Text="Sonic Adventure"></asp:ListItem>
</asp:DropDownList>
</p>
</div>
</form>
</body>
</html>
9. While in Default.aspx, switch to Design view. Double click the button Add.
This takes us to Default.aspx.vb, the Visual Basic coding of the page. You should see a Button1_Click method. Inside that method we need to tell it to add what the user wrote in the text box into the dropdown list.
10. Inside the Button1_Click method, write DropDownList1.Items.Add (TextBox1.Text).
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.
Code Block
Default.aspx.vb
We gain access to the items in the drodown list by typing DropDownList1.Items. After we access the items we can add more items by using the .Add.Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
DropDownList1.Items.Add(TextBox1.Text)
End Sub
End Class
Run the application and click on the dropdown list. There will be two items, Shenmue and Sonic Adventure. Now write anything in the text box and click Add. Now open the dropdown list again and you should see what you wrote on the list. Keep in mind this tutorial does not show how to validate data in the text box and duplicates in the dropdown list, so that will occur. Just grasp the basics of setting and adding to a dropdown list.
WebSite8.zip
