TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net DropDownList

ASP.Net DropDownList with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET DropDownList

The DropDownList is a web server control which is used to create an HTML Select component. It allows us to select an option from the dropdown list. It can contain any number of items

ASP.NET provides a tag to create DropDownList for web application. The following is the Syntax of DropDownList tag.

<asp:DropDownList id="DropDownList1" runat="server"
     DataSource="<% databindingexpression %>"
     DataTextField="DataSourceField"
     DataValueField="DataSourceField"
     AutoPostBack="True|False"
     OnSelectedIndexChanged="OnSelectedIndexChangedMethod">
   <asp:ListItem value="value" selected="True|False">
      Text
   </asp:ListItem>
</asp:DropDownList>

ASP.NET DropDownList Example

We are creating DropDownList by using Visual Studio 2017. This example includes the following steps.

Create a Web Form

Add a new form by specifying its name.

ASP Net Dropdown list 1

Initially, it is an empty form. Now, we will add a new DropDownList by dragging it from the toolbox.

ASP Net Dropdown list 2

After dragging, our web form looks like the below.

ASP Net Dropdown list 3

Now, to add items to the list, visual studio provides Items property where we can add items. The property window looks like this.

ASP Net Dropdown list 4

Click on the items (collection) and it will pop up a new window as given below. Initially, it does not have any item. It provides add button to add new items to the list.

ASP Net Dropdown list 5

Adding item to the DropDownList, by providing values to the Text and Value properties.

ASP Net Dropdown list 6

We have added more items to it and now, it looks like the following.

ASP Net Dropdown list 7

After clicking OK,


DropDownListExample.aspx

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" 
CodeBehind="Default.aspx.cs" Inherits="DropDownListExample._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <p>Select a City of Your Choice</p>
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" >
            <asp:ListItem Value="">Please Select</asp:ListItem>
            <asp:ListItem>New Delhi </asp:ListItem>
            <asp:ListItem>Greater Noida</asp:ListItem>
            <asp:ListItem>NewYork</asp:ListItem>
            <asp:ListItem>Paris</asp:ListItem>
            <asp:ListItem>London</asp:ListItem>
        </asp:DropDownList>
        </div>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
    </form>
</body>
</html>

// DropDownListExample.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DropDownListExample
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "")
            {
                Label1.Text = "Please Select a City";
            }
            else
                Label1.Text = "Your Choice is: " + DropDownList1.SelectedValue;
        }
    }
}

Output:

ASP Net Dropdown list 8

At server side, selected city is fetched and display to the user.

ASP Net Dropdown list 9
Next TopicASP.NET DataList




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf