Different types of data binding in Dropdownlist


In this article, I will explain various types of data can be binding in dropdownlist. Description about each type of data for binding in dropdownist. There are different types of data can be bind in dropdownlist. Some types are binding direct data to the dropdownlist, using list and using Sql data

Types of binding data in Dropdownlist



There are various types of data can be binding in dropdownlist. Some are below

Different type of binding data in Dropdownlist


1,Direct
2,Using list
3,using Sql data


1,Direct data binding
Bind the data code directly into the design page itself. By using value and Text field in the dropdownlist, we can bind the data to dropdownlist.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>Dropdownlist Sample One</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbllang" runat="server" Text="Language :"></asp:Label>
<asp:DropDownList ID="ddllangval" runat="server">
<asp:ListItem Value="English" Text="English"></asp:ListItem>
<asp:ListItem Value="English" Text="French"></asp:ListItem>
<asp:ListItem Value="English" Text="German"></asp:ListItem>
<asp:ListItem Value="English" Text="Tamil"></asp:ListItem>
<asp:ListItem Value="English" Text="Telugu"></asp:ListItem>
<asp:ListItem Value="English" Text="Malayalam"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>



2,List data binding

Using List you can bind the dropdownlist data. In codebehind you have to write to code to bind the dropdownlist. First add the data to list and then bind the data to dropdownlist DataSource.

Design page code as follows


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>Sample Two Dropdownlist Binding</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblLang" runat="server" Text="Language :"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>


code behind code as follows


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List list = new List();
list.Add("English");
list.Add("French");
list.Add("German");
list.Add("Tamil");
list.Add("Telugu");
list.Add("Malayalam");
this.DropDownList1.DataSource = list;
this.DropDownList1.DataBind();
}
}



3,Sql data
First declare sqlconnection object and pass sqlcommand with sql select statement and bind data into dataset using sqldataadapter. Dataset data can be binded to dropdownlist.

Design page code as follows

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>Sample 3 Dropdownlist Binding using Sql Data</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>



code behind code as follows


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create sqlconnection and pass connection string for database as parameter
using (SqlConnection con = new SqlConnection("Data Source=sampledatasource;Integrated Security=true;Initial Catalog=Sampledb"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select langid,languagename FROM language", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "languagename";
DropDownList1.DataValueField = "langid";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select", "0"));
con.Close();
}
}
}


I had attached all three samples for the reference


Attachments

  • Sample one direct data binding in dropdownlist (44518-204416-Sample-one-direct-data-binding-dropdownlist.zip)
  • Sample two List data binding in Dropdownlist (44518-20457-Sample-Two-List-data-binding-Dropdownlist.zip)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: