Category Archives: DropDownList Control

Country State City Cascading DropDownList with Database in ASP.Net

Cascading DropDownList Control with Database in ASP.Net C#.

In previous asp.net article, we have discussed about how to display cascading dropdownlist control without database in asp.net.

In this asp.net tutorial we will learn how to display cascading dropdownlist control using database in asp.net. For display values in dropdownlist control, first we need to insert data into database. Here, we use sql server database, first create a new database and create three table for County data, State Data and last one for City Date.

The Database table design should be like :

TableName = CountryMst
Columns :  CID (primary key / AutoIncrement) , CountyName as Nvarchar(200)

TableName = StateMst
Columns : SID (primary key / AutoIncrement), StateName as Nvarchar(200), CID as int

Table Name = CityMst
Columns : CID (primary key / AutoIncrement), CityName as Nvarchar(200), SID as int

Here is the screen short of sql server database table.

Table Design for CountryMst

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net C#

Table Design for StateMst

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net C#

Table Design for CityMst

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net C#

Here, below screen we can see the Database Example contains three table CountryMst, StateMst and CityMst with columns  and datatype in sql server.

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

Now, After Design Database table it’s time to create a stored procedure for manipulate database table.

Create New Stored procedure :

Database –> Example –> Programmability — > Stored Procedure

Here is code for all stored procedure we have created for our asp.net example.

Stored Procedure : COUNTRY_SELECT

 Stored Procedure : COUNTRY_INSERT

 Stored Procedure  : STATE_SELECT

  Stored Procedure  : STATE_INSERT

   Stored Procedure  : STATE_SELECT_BY_CID

 Stored Procedure  : CITY_SELECT

 Stored Procedure  : CITY_INSERT

Stored Procedure :  CITY_SELECT_BY_SID

 

Now, Create a new asp.net web application and design a web forms for inset data into sql server database.

First Design asp.net web page like below for insert country, state and city data.

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

The HTML Source code for above web page design is :

In this asp.net cascading dropdownlist example we use MultiView control for make user friendly design to easily understand. we took here three view control each for county, state and city inside the multiview control.

Now, first we have to insert country data then after state data and finally insert city data according to inserted data.

Here, the first output screen of insert data in to table for country, state and city.

Cascading dropdownlist in asp.net

Cascading dropdownlist in asp.net

In above screen there are three button control for display view control one by one as per button click.

First import three namespace on code behind page for sql connectivity.

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

Write a connection string code above page_load events :
Here, we have created a private function bindconnection for selecting data from database using SELECT Storedprocedure.

string strconn = “Data Source=.\\NEWSQLEXPRESS;Initial Catalog=’EXAMPLE’;Integrated Security=True”;
protected void Page_Load(object sender, EventArgs e)
{

}

private DataTable bindconnection(string queryname)
{

SqlConnection sqlconn = new SqlConnection(strconn);
SqlCommand sqlcmd = new SqlCommand(queryname, sqlconn);

SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);
DataTable DT = new DataTable();
adapter.Fill(DT);
return DT;
}

The asp.net c# code behind code for  ADDCounty Button control click event.

The C# Code for ADD State Button :

The C# Code for ADD City Button :

Now, when we select the add country button then the only view1 is displayed for insert country data in to CountryMst table in database.

Add New County Data in to CountyMst table in sql server :

Cascading dropdownlist in asp.net

Add New County Data in to CountryMst Table

The C# code for ADD button to insert new country records.

Now, After adding some country records insert new state record respectively to country records.

Cascading dropdownlist in asp.net

Add New State Data in to StateMst Table

The C# code for ADD State records :

Now, Add new city records as per the selected country and state record.

Cascading dropdownlist in asp.net

Add New City Data in to CityMst Table

Cascading dropdownlist in asp.net

Add New City Data in to CityMst Table

Here, when we select country from county dropdownlist then the related state will be displayed automatically in state dropdownlist control. for do this write a code behind c# code on Country Dropdownlist SelectedIndexChanged and set AutoPostBack = True

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

The code for County Dropdownlist SelectedIndexChnaged events :

The C# Code for Add new City Records :

Now, up to this we have successfully added country, state and city records in database.

Let’s take an example to understand cascading dropdownlist from database. In above example we have insert records in to databse for country, state and city. Now desing an asp.net web page for cascading dropdownlist for country, state and city. In cascading drodownlist example we display records respectively from database, which we alreay inserted in above example.

First Design asp.net web page for cascading dropdownlist example.

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

The HTML Source code for cascading dropdownlist example is :

For display state recordes according to select country records and display city records according to select country and state record, we need to set AutoPostBack = true for Country and State Dropdownlist control.

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

Import  namespace and write connection string code like :

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

string strconn = “Data Source=.\\NEWSQLEXPRESS;Initial Catalog=’EXAMPLE’;Integrated Security=True”;
protected void Page_Load(object sender, EventArgs e)
{

}

write below code in page_load events for bind country dropdownlist control from databse.

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

The C# code for County Dropdownlist SelectedIndexChanged events :

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

The C# code for State Dropdownlist SelectedIndexChanged events :

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

Country State City DropDownList in ASP.Net

I hope this asp.net country, state and city cascading dropdownlist from database example will help you…..

Country, State and City DropDownList Example in ASP.Net C#

County, State and City cascading dropdownlist control example

The dropdownlist control is used to store multiple items in asp.net. The country, state and city dropdownlist example means, we have three dropdownlist control one for each country, state and city.

The first dropdownlist is for country, if we select country from dropdownliste then related state name automatically displayed in second dropdownlist. If we select any state from state dropdownlist then the city which is related to selected state will be displayed in third city dropdownlist control.

This type of dropdownlist example we can say cascading dropdownlist.

Let’s understand cascading dropdownlist example with country, state and city.

First create a web application in asp.net with default.aspx web page.

Now design asp.net web page with three dropdownlist control with two button control shows like below screen.

Here is the html source code for asp.net default.aspx page.

Here is the layout of asp.net web page for cascading dropdownlist example.

Here, we have taken three dropdownlist control one for country, second for state and third for city.

In the county dropdownlist insert manually county name and select county name and click select button to display related sate name in second dropdownlist.

Cascading dropdownlist

Cascading dropdownlist control for country, state and city in asp.net C#

Here is the code for two button control click events.

The Country Button Code :

The State Button Code :

Cascading dropdownlist

Cascading dropdownlist control for country, state and city in asp.net C#

Below asp.net figure shows we have select country INDIA and state Gujarat then related city will be automatically displayed in third city dropdownlist control.

Cascading dropdownlist

Cascading dropdownlist control for country, state and city in asp.net C#

We have make proper validation for selection, if you don’t select any county from country dropdownlist then you will get error message like Select Proper County !!.

Cascading dropdownlist

Cascading dropdownlist control for country, state and city in asp.net C#

If we don’t select proper state then the error message look like Select Proper State!!.

Cascading dropdownlist

Cascading dropdownlist control for country, state and city in asp.net C#

Cascading Dropdownlist Example using AutoPostBack

In above example we have taken two button control for select country and select state. Now we have make same cascading example without taking button control.

The AutoPostBack Property of DropDownlList control

The Autopostback property of dropdownlist control allow user to execute code behind code while changing selected item from dropdownlist control in asp.net. If we use autopostback then in above example we don’t need to use button control for server code.

we can write server code on SelectedIndexChanged events of dropdownlist control.

we take above same cascading dropdown example using autopostback in asp.net.

Here we took only three dropdownlist control for country, state and city. we write server side code on dropdownlist SelectedIndexChanged events instead of button click events.

Cascading dropdownlist

Cascading dropdownlist using autopostback for country, state and city in asp.net C#

Now, after designing asp.net web page, set AutoPostBack = true for country and state dropdownlist control.

Cascading dropdownlist

Cascading dropdownlist using autopostback for country, state and city in asp.net C#

Now go to the SelectedIndexChanged events of country dropdownlist for write server code.

Code for country SelectedIndexChanged events :

Code for State SelectedIndexChanged events:

 

Cascading dropdownlist

Cascading dropdownlist using autopostback for country, state and city in asp.net C#

 

Cascading dropdownlist

Cascading dropdownlist using autopostback for country, state and city in asp.net C#

 

 

Validate DropDownlList Control unsing RequiredField Validator in ASP.Net

In this asp.net post we will learn how to validate dropdownlist control using requiredfield validator control in asp.net.

For understand validate dropdownllist control in asp.net using required field control create a asp.net web for with one dropdownlist control, button control along with requiredfield control.

take dropdownlist control and add some items in dropdownlist, In dropdownlist control we must select item from second to last, means requiredfiled validator control show the error message, if we select the first item from dropdownlist.

Here is the design code for dropdownlist example:

– design code for requiredfiled validator control:

here, we set two property of requiredfield validator
– ControlToValidator=”DropDownList1″
-InitialValue=”SELECT” <– Fist value of dropdownlist control

Here, is the output of dropdownlist control validate asp.net example:

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Validate Dropdownlist control using RequiredField Validator control in ASP.Net

Create Login Form in ASP.Net, Login Name match with Items of DropDownList control in ASP.Net

Here, we are going to learn match textbox control text value with any Items of DropDownList control,  if match display message login success other wise login fail.

Example: Here we have one DropDownList control which have five Items named “Meera”, “Academy”, “ASP”, “PHP” and “SQL”. and we have one TextBox Control, here we match the TextBox control Text Value with any Items of DropDownLsit  control then message display success other wise fail.

for this ASP.Net Example we need to use FOR Loop her to solve this example.

First Design asp.net web page with HTML code:

<table style=”width: 416px”>
<tr>
<td style=”width: 111px; text-align: right”>
UserName :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpusername” runat=”server” Width=”128px”>
<asp:ListItem>Meera</asp:ListItem>
<asp:ListItem>Academy</asp:ListItem>
<asp:ListItem>ASP</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
Enter UserName :</td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”txtusername” runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
</td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”btnlogin” runat=”server” Font-Bold=”True” Height=”32px” OnClick=”btnlogin_Click”
Text=”Login” Width=”80px” /></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 111px; text-align: right”>
</td>
<td colspan=”2″ style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
</tr>
</table>

The ASP.Net Example of DropDownList Control output Design output is:

match textbox text with dropdownllist control any item in asp.net

match textbox text with dropdownllist control any item in asp.net

Write the below code on Login Button Click event at asp.net code behind page.

protected void btnlogin_Click(object sender, EventArgs e)
{
for (int i = 0; i < Drpusername.Items.Count; i++)
{
if (txtusername.Text == Drpusername.Items[i].Text)
{
Label1.Text = “Login Successfull !!”;
break;
}
Label1.Text = “Invalid USerName !!”;
}
}

 

If TextBox control will be blank then the error  message like Enter UserName  in below screen.

match textbox text with dropdownllist control any item in asp.net

match textbox text with dropdownllist control any item in asp.net

If your TextBox Text value not int the DropDownList control then error message like “Invalid UserName”.

match textbox text with dropdownllist control any item in asp.net

match textbox text with dropdownllist control any item in asp.net

If the TextBox Text Value match with any items of DropDownList control then success message display Login Successfull.

match textbox text with dropdownllist control any item in asp.net

match textbox text with dropdownllist control any item in asp.net

I hope this asp.net Example of DropDownList control will help you……

Create Login Form in ASP.Net, Login Name match with Selected Item of DropDownList control in ASP.Net

Here, we are going to learn match textbox control text value with DropDownList control Selected Item, if match display message login success other wise login fail.

First Design asp.net web page with HTML code:

<table style=”width: 416px”>
<tr>
<td style=”width: 100px; text-align: right”>
Select UserName :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpvalue” runat=”server” Width=”128px”>
<asp:ListItem>SELECT</asp:ListItem>
<asp:ListItem>Meera</asp:ListItem>
<asp:ListItem>Academy</asp:ListItem>
<asp:ListItem>ASP</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
Enter USerName :</td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”txtvalue” runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; height: 6px; text-align: right”>
</td>
<td style=”width: 100px; height: 6px; text-align: left”>
<asp:Button ID=”btnsubmit” runat=”server” Font-Bold=”True” Height=”32px” OnClick=”btnsubmit_Click”
Text=”SUBMIT” Width=”80px” /></td>
<td style=”width: 100px; height: 6px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td colspan=”2″ style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
</tr>
</table>

The ASP.Net DropDownList control output is:
If We don’t select Item from DropDownList control then error message will be display like Select First UserName:

match textbox text with dropdownllist control selected item in asp.net

match textbox text with dropdownllist control selected item in asp.net

Now, write the below code on SUBMIT Button click events at code behind in asp.net

protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Drpvalue.SelectedIndex == 0)
{
Label1.Text = “Select First UserName !”;
Drpvalue.Focus();
}
else
{
if (txtvalue.Text == “”)
{
Label1.Text = “Enter UserName !”;
txtvalue.Focus();
}
else
{
if (txtvalue.Text == Drpvalue.SelectedItem.Text)
{
Label1.Text = “UserName is write !!”;
}
else
{
Label1.Text = “USerName is Wrong !!”;
txtvalue.Focus();
}

}
}

The ASP.Net Example of DropDownList out put is:
If we have select username from DropDownList control and TextBox control doesn’t has a value then error message display like Enter your UserName.

match textbox text with dropdownllist control selected item in asp.net

match textbox text with dropdownllist control selected item in asp.net

The ASP.Net Example of DropDownList out put is:

If both DropDownList Selected Item and TextBox Text Value not match then error message display UsrName is Wrong.

match textbox text with dropdownllist control selected item in asp.net

match textbox text with dropdownllist control selected item in asp.net

The ASP.Net Example of DropDownList out put is:

If both are same then message will be UserName is write….

match textbox text with dropdownllist control selected item in asp.net

match textbox text with dropdownllist control selected item in asp.net

I hope this ASP.Net Example of DropDownList control will help you…….

How to use Clear, Remove and RemoveAt in DropDownList in ASP.Net

Here in this ASP.NET Example of DropDownList Control we will learn to use Clear(), Remove() and RemoveAt() method in ASP.Net with C# Programmatically.

First we design the ASP.Net web page with One DropDownList Control and Three Button Control.

The HTML Code for asp.net web page is:

<table>
<tr>
<td style=”width: 100px; text-align: right”>
Select Value :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpvalue” runat=”server”
Width=”128px”>
<asp:ListItem Value=”0″>SELECT</asp:ListItem>
<asp:ListItem Value=”Meera”>Meera</asp:ListItem>
<asp:ListItem Value=”Academy”>Academy</asp:ListItem>
<asp:ListItem Value=”ASP”>ASP</asp:ListItem>
<asp:ListItem Value=”PHP”>PHP</asp:ListItem>
<asp:ListItem Value=”SQL”>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”CLEAR” /></td>
<td style=”width: 100px”>
<asp:Button ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”REMOVE” /></td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”Button3″ runat=”server” OnClick=”Button3_Click” Text=”REMOVE AT” /></td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: center”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True” ForeColor=”Purple”></asp:Label></td>
</tr>
</table>

The Design output of SAP.Net Example is:

 Use Clear() in DropdownList control in ASP.Net

Use Clear() in DropdownList control in ASP.Net

write the below code on CLEAR Button for clear all the items from DropDownList.

 protected void Button1_Click(object sender, EventArgs e)
{
Drpvalue.Items.Clear();
Label1.Text = “All Items Clear !”;
}

The ASP.NET DropDownList Control Clear() Method output is:

Clear Method in DropDownList in ASP.Net with C#

Clear Method in DropDownList in ASP.Net with C#

Now, For Remove a Item write below code on REMOVE Button Click Event:

protected void Button2_Click(object sender, EventArgs e)
{
Drpvalue.Items.Remove(“ASP”);
Label1.Text = “‘ASP’ Removed Successfully”;
}

The ASP.Net DropDownList Control Remove method output is:

use Remove in DropDownList control in ASP.Net with C#

use Remove in DropDownList control in ASP.Net with C#

Write Below code on ClearAt Button for Clear Item from particular Index in ASP.Net.

protected void Button3_Click(object sender, EventArgs e)
{
Drpvalue.Items.RemoveAt(2);
Label1.Text = “Item Removed from Position 2 !”;
}

The ASP.NET DropDownList control RemoveAt method output is:

use RemoveAt in DropDownList control in ASP.Net with C#

use RemoveAt in DropDownList control in ASP.Net with C#

I hope this asp.net CLear(), Remove() and RemoveAt() Method of DropDownList control will help you….

Enable, Disable, Visible and Hide DropDownList Example in ASP.Net

Here, we will learn about Enable, Disable, Visible and Hide Web Server control depending of Selection of DropDownList control in asp.net with c#.

When we select Disable from DropDownList control the rest of the all control will be Disabled, same things work on Visible, Hide and Enable as well.

Now, Design the asp.net web page for Example :

 <table>
<tr>
<td style=”width: 100px; text-align: right”>
Seelct Value :</td>
<td style=”width: 100px”>
<asp:DropDownList ID=”DropDownList1″ runat=”server” Width=”128px”>
<asp:ListItem>Enable</asp:ListItem>
<asp:ListItem>Disable</asp:ListItem>
<asp:ListItem>Visible</asp:ListItem>
<asp:ListItem>Hide</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Result” /></td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: right”>
&nbsp; &nbsp;
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
<asp:Button ID=”Button2″ runat=”server” Text=”SUBMIT” /></td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”DropDownList2″ runat=”server”>
<asp:ListItem>ABC</asp:ListItem>
<asp:ListItem>XYZ</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: center”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True” ForeColor=”Purple”></asp:Label></td>
</tr>
</table>

The ASP.Net Example of DropDownList Control output is:

Enable DropDwonList control in asp.net with C#

Enable DropDwonList control in asp.net with C#

write the below code on Result Button Click Event for Enable, Disable, Visible and Hide Web server control:

protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
Button2.Visible = true;
TextBox1.Visible = true;
DropDownList2.Visible = true;

Button2.Enabled = true;
TextBox1.Enabled = true;
DropDownList2.Enabled = true;
Label1.Text = “ALL Controls are Enable”;
}
else if (DropDownList1.SelectedIndex == 1)
{
Button2.Enabled = false;
TextBox1.Enabled = false;
DropDownList2.Enabled = false;
Label1.Text = “ALL Controls are Disable”;
}
else if (DropDownList1.SelectedIndex == 2)
{
Button2.Visible = true;
TextBox1.Visible = true;
DropDownList2.Visible = true;
Label1.Text = “ALL Controls are Visible”;
}
else if (DropDownList1.SelectedIndex == 3)
{
Button2.Visible = false;
TextBox1.Visible = false;
DropDownList2.Visible = false;
Label1.Text = “ALL Controls are Hide/Invisible”;
}
}

After writing above code your example will work Enable, Disable, Visible and Hide as per selected Item of DropDownList control.

The output of Disable all control is:

Enable, Disable in asp.net with C#

Enable, Disable in asp.net with C#

The ASP.Net DropDownList Control Example is:

 Enable, Disable, Visible, Hide in asp.net with C#

Enable, Disable, Visible, Hide in asp.net with C#

The ASP.Net DropDownList Control Example output is:

Enable, Disable, Visible, Hide in asp.net with C

Enable, Disable, Visible, Hide in asp.net with C

I hope this asp.net tutorials example of Enable, Disable, Visible, Hide will help you…..

How to get total count of Items in DropDownList Control in ASP.Net

Here, we will learn how to get total number of count of Items in DropDownList control in ASP.Net C#.

We will learn with an Example of asp.net C#.

First Design the asp.net web form:

 <table>
<tr>
<td style=”width: 100px; text-align: right”>
Select Value :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpvalue” runat=”server” Width=”128px”>
<asp:ListItem Value=”0″>SELECT</asp:ListItem>
<asp:ListItem Value=”Meera”>Meera</asp:ListItem>
<asp:ListItem Value=”Academy”>Academy</asp:ListItem>
<asp:ListItem Value=”ASP”>ASP</asp:ListItem>
<asp:ListItem Value=”PHP”>PHP</asp:ListItem>
<asp:ListItem Value=”SQL”>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
&nbsp;</td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”COUNT” /></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: center”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True” ForeColor=”Purple”></asp:Label></td>
</tr>
</table>

The ASP.Net example of DropDownList control output is:

Get Count of DropDownList Items in ASP.Net C#

Get Count of DropDownList Items in ASP.Net C#

Now, write the below code on Button Click events :

    protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = “Total Record = “+ Drpvalue.Items.Count.ToString();
}

I hope this asp.net tutorials will help you..

Transfer DropDownList All Items to another DropDownList Control in ASP.Net

Here, In asp.net tutorials of dropdownlist control we will learn how to move or transfer all the Items of DropDownList control to another DropDownList control programmatically in asp.net with C#.

Here, we need to use for loop for all all items to another DropDownList control on Button Click Events.

We use DropDownList1.Items.Add(“ItemName”) for add new item to dropdownlist control in asp.net.

Exmaple:

First Design the asp.net web form like below:

<table>
<tr>
<td style=”width: 100px; text-align: right; height: 24px;”>
Value :</td>
<td style=”width: 100px; height: 24px;”>
<asp:DropDownList ID=”DropDownList1″ runat=”server” Width=”128px”>
<asp:ListItem>ABC</asp:ListItem>
<asp:ListItem>DEF</asp:ListItem>
<asp:ListItem>Meera</asp:ListItem>
<asp:ListItem>Academy</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; height: 24px;”>
</td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: right”>
&nbsp; &nbsp;
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Transfer to single” /><asp:Button
ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”Transfer to Both” /></td>
</tr>
<tr>
<td style=”width: 100px; text-align: right; height: 24px;”>
</td>
<td style=”width: 100px; text-align: left; height: 24px;”>
<asp:DropDownList ID=”DropDownList2″ runat=”server” Width=”128px”>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left; height: 24px;”>
<asp:DropDownList ID=”DropDownList3″ runat=”server” Width=”128px”>
</asp:DropDownList></td>
</tr>
</table>

The Design output of asp.net dropdownlist control example is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

Now, Here we have two button control, when we click Transfer to Single that means all items transfer to only one DropDownList control and click Transfer to Both the all Items transfer to both DropDownList Contol in asp.net C#.

write below C# code at Transfer to Single Button click event:

protected void Button1_Click(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList3.Items.Clear();
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
DropDownList2.Items.Add(DropDownList1.Items[i].Text);
}
}

The asp.net DropDownList control Transfer Item Example output is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

After doing this, write below code at Transfer to Both Button click event at coding page of asp.net webform.

protected void Button2_Click(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
DropDownList3.Items.Clear();

for (int i = 0; i < DropDownList1.Items.Count; i++)
{
DropDownList2.Items.Add(DropDownList1.Items[i].Text);
DropDownList3.Items.Add(DropDownList1.Items[i].Text);
}
}

The asp.net DropDownList control Transfer Item Example output is:

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

Transfer Items from one DropDownList control to other DropDownList Control in asp.net

I hope this asp.net DropDownList control example will help you……..

How to use SelectedIndexChanged of DropDownList in ASP.Net

The DropDownList control is mostly used in asp.net for store multiple Items with it.
In DropDownList control we can store multiple Items but we can select maximum one Item from DropDownList Control.

Here, we are going to lean DropDownList control SelectedIndexChanged Event in asp.net.
When we select any item from DropDownList at that time the SelectedIndexChanged Event fired.

If we want to write or do something when dropdownlist item selection changed, so we can write code on dropdownlist SelectedIndexChange Events.
But we first set AutoPostBack=True for fire SelectedIndexChanged Event in asp.net C#.

Now we have a Example of DropDownList for understanding the SelectedIndexChanged in asp.net with C#.

Here, we have two DropDownList control on Webform in asp.net, First DropDownList has a Colletion of Country Name and Second has a Collection State Name.

Now, when we select Country from first DropDownList the Related State will be displayed in Second DropDownList control on SlectedIndexChanged Event of first DropDownList Control in asp.net.

First we design the asp.net web page with below HTML code:

<table>
<tr>
<td style=”width: 100px; height: 21px”>
</td>
<td style=”width: 100px; height: 21px”>
</td>
<td style=”width: 100px; height: 21px”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
Select Country :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”DrpCountry” runat=”server” AutoPostBack=”True” OnSelectedIndexChanged=”DrpCountry_SelectedIndexChanged”
Width=”128px”>
<asp:ListItem Value=”0″>SELECT</asp:ListItem>
<asp:ListItem Value=”1″>INDIA</asp:ListItem>
<asp:ListItem Value=”2″>USA</asp:ListItem>
<asp:ListItem Value=”3″>UK</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
State :
</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”DrpState” runat=”server” Width=”128px”>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
</table>

The ASP.NET DropDownList control Example output is:

DropDownlist control SelectedIndexChanged event in asp.net with C#

DropDownlist control SelectedIndexChanged event in asp.net with C#

Now, After Design a page go to the DropDownList control SelectedIndexChanged Event by Clicking the DropDownList.

Befor write code on DropDownList SelectedIndexChanged
set AutoPostBack=True of DropDownList control.

protected void DrpCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (DrpCountry.SelectedItem.Text == “SELECT”)
{
DrpState.Items.Clear();
}
else if (DrpCountry.SelectedItem.Text == “INDIA”)
{
DrpState.Items.Clear();
DrpState.Items.Add(“Gujarat”);
DrpState.Items.Add(“Maharastra”);
DrpState.Items.Add(“Rajestan”);
}
else if (DrpCountry.SelectedItem.Text == “USA”)
{
DrpState.Items.Clear();
DrpState.Items.Add(“New Jearsy”);
DrpState.Items.Add(“Abc”);

}
else if (DrpCountry.SelectedItem.Text == “UK”)
{
DrpState.Items.Clear();
DrpState.Items.Add(“London”);
DrpState.Items.Add(“Manchester”);
}
}

The ASP.NET DropDownList control SelectedIndexChanged output is:

DropDownlist control SelectedIndexChanged event in asp.net with C#

DropDownlist control SelectedIndexChanged event in asp.net with C#

In above figure shows, The State displayed related to the Country of first DropDownList in asp.net with C#.

I hope this asp.net tutorials of dropdownlist control will help you…..

How to use AutoPostBack Property in DropDownList Control in ASP.Net

Here, we are going to learn about autopostback property in asp.net with the dropdownlist control.

AutoPostBack of the ASP.Net DropDownList control provides the functionality to allow the server side post back event of the web page when user Select the DropDownlist Item from control while running programm.

Here we will make an example for understand Autopostback property.
When we select Item from DropDownlist the Selected Item will be Displayed in TextBox Control in ASP.Net C#.

First we design the asp.net web page with one textbox control along with the one  DropDwonlist control.

The HTML Design code for asp.net web page is like:

<table>
<tr>
<td style=”width: 100px; text-align: right”>
Select Value :</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”Drpvalue” runat=”server” AutoPostBack=”True” OnSelectedIndexChanged=”Drpvalue_SelectedIndexChanged”
Width=”128px”>
<asp:ListItem Value=”0″>SELECT</asp:ListItem>
<asp:ListItem Value=”1″>Meera</asp:ListItem>
<asp:ListItem Value=”2″>Academy</asp:ListItem>
<asp:ListItem Value=”3″>ASP</asp:ListItem>
<asp:ListItem Value=”4″>PHP</asp:ListItem>
<asp:ListItem Value=”5″>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
Answer :
</td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
</table>

The ASP.Net Autopostback example of DropDownlist control design output is :

DropDownlist control Autopostback property in asp.net with c#

DropDownlist control Autopostback property in asp.net with c#

Now, open the property windows of DropDownlist control and change the Autopostback property.

AutoPostBack = True

write below code on DropDownList SelectedIndexChanged Event on code behind page:

protected void Drpvalue_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = Drpvalue.SelectedItem.Text;
}

After writing this coding, when you select Item from dropdownlist the Item display in the TextBox control because we changed the AutoPostBack=True of DropDownlList control.

The ASP.Net Autopostback DropDownlist control output:

DropDownlist control Autopostback in asp.net c#

DropDownlist control Autopostback in asp.net c#

I hope this asp.net dropdownlist autopostback event example will help you….

How to Add and Insert Item in DropDownList contol in ASP.Net

Here, in this asp.net post we will learn to Add and Insert Item in DropDownlist control in asp.net c#.

First we learn the difference between Add and Insert property of dropdownlist control.

Add – Add property used to Add New Item in DropDownList control at the End of the List means add new Item at last position to the dropdownlist control.

Insert – Insert property used to Insert New Item in DropDownList at Desired specific position in the list.

First we design the asp.net webpage with two TextBox Control and two Button Control and one label control.

The HTML code for design asp.net page is:

<table>
<tr>
<td style=”width: 100px; text-align: right”>
&nbsp;Enter Value :</td>
<td style=”width: 100px; text-align: left”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
&nbsp;</td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”ADD” />
&nbsp;
<asp:Button ID=”Button2″ runat=”server” OnClick=”Button2_Click” Text=”INSERT” /></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td style=”width: 100px; text-align: left”>
<asp:DropDownList ID=”DropDownList1″ runat=”server” Width=”128px”>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td colspan=”3″ style=”text-align: center”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True” ForeColor=”Purple”></asp:Label></td>
</tr>
</table>

The output of asp.net example of add and insert item to dropdownlist is:

add / insert new item to dropdownlist contol in asp.net

add / insert new item to dropdownlist contol in asp.net

Now, write the below code on ADD button for Add new item to Dropdownlist control at last position.

protected void Button1_Click(object sender, EventArgs e)
{
DropDownList1.Items.Add(TextBox1.Text);
Label1.Text = TextBox1.Text + ” Item added to end of list”;
}

The Out put of asp.net add new item to dropdownlist contol is:

Add / Insert new item to dropdownlist control in asp.net

Add / Insert new item to dropdownlist control in asp.net

Write below code on INSERT button for insert new item at specific position in dropdownlist control in asp.net.

protected void Button2_Click(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0, TextBox1.Text);
Label1.Text = TextBox1.Text + ” Item added at position 0″;
DropDownList1.SelectedIndex = 0;
}

The asp.net dropdownlist insert example output like:

Add / Insert new item to dropdownlist control in asp.net

Add / Insert new item to dropdownlist control in asp.net

I hope this asp.net post of add / insert item to dropdownlist control will hepl you…..