DropDownList Example in ASP.NET using C#

DropDownList Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 22, 2010 at 4:48 PM Comments : [ 0 ]

In this article we will introduce with DropDownList control in ASP.NET using C#.

DropDownList Example in ASP.NET using C#

DropDownList control is know as ComboBox control. DropDownList control contains multiple of items but the user can choose only one item at a time. The functionality of DropDownList is like a Single Row List Box. The DropDownList control is provided as a server control. In this example we have two TextBoxes. If you enter the value in Textboxes and select the operation from DropDownList the result will be shown in label. We have true to Enable AutoPostBack because If it is true, the form is automatically posted back to the server when user select any of the item from DropDownList.
The following figure shows how we can edit items in DropDownList.

DropDownList.aspx (Design Page):

DropDownList.aspx (source code):


<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master"
AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="DropDownList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style3
{
color: #800000;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">DropDownList in ASP.NET 4 , C#</h2>
<strong><span class="style3">Enter first number:</span>
</strong>
<br />
<asp:TextBox ID="txt1" runat="server" Text="12"/>
<br />
<br />
<span class="style3">
<strong>Enter second number:
</strong>
</span>
<br />
<asp:TextBox ID="txt2" runat="server" Text="3" />
<br />
<br />
<span class="style3">
<strong>Select operation:
</strong>
</span>
<br />
<asp:DropDownList ID="drp1" runat="server" Width="145px" 
Font-Bold="True" ForeColor="#006666" 
onselectedindexchanged="drp1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>----Select----</asp:ListItem>
<asp:ListItem>Addition</asp:ListItem>
<asp:ListItem>Subtraction</asp:ListItem>
<asp:ListItem>Multiplication</asp:ListItem>
<asp:ListItem>Division</asp:ListItem>
</asp:DropDownList>
</div>
<br />
<asp:Label ID="label1" runat="server" Font-Bold="True" ForeColor="#000099" />
</asp:Content>

DropDownList.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DropDownList : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void drp1_SelectedIndexChanged(object sender, EventArgs e)
{
double firstno = Convert.ToDouble(txt1.Text);
double secondno = Convert.ToDouble(txt2.Text);
if(drp1.SelectedIndex == 1)
{
double add = firstno + secondno;
label1.Text = "Addition is :" + add;
}
if (drp1.SelectedIndex == 2)
{
double sub = firstno - secondno;
label1.Text = "Subtraction is :" + sub;
}
if (drp1.SelectedIndex == 3)
{
double mul = firstno * secondno;
label1.Text = "Multiplication is :" + mul;
}
if (drp1.SelectedIndex == 4)
{
double div = firstno / secondno;
label1.Text = "Division is :" + div;
}
}
}

Output:

Download source code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics