Blue Theme Orange Theme Green Theme Red Theme
 
The Top 5 .NET Memory Management Fundamentals
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » ASP.NET & Web Forms » How to Bind Images With DropDownList in ASP.NET

How to Bind Images With DropDownList in ASP.NET

Here, we will learn how to bind images with a DropDownList in ASP.NET.

Author Rank :
Page Views : 1071
Downloads : 46
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Bindimageswithdropdownlist.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
The Top 5 .NET Memory Management Fundamentals
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Here, we will learn how to bind images with a DropDownList in ASP.NET. To do that we create an image folder in the application and insert some images in the folder. After that we create a database table in SQL Server and insert an image name in the database with image name.

Creating Table in SQL Server Database

Now create a table named CountryImage with the columns Countryid, CountryName and CountryImage. The table looks as below.

ddl1.gif

Insert an image name in the table which is defined in the application's image folder.

ddl2.gif

Now create a new web application project in Visual Studio 2010. Drag and Drop a DropDownList to the form. For a sample design your aspx page might be like this:

aspx page

<%@ 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 id="Head1" runat="server">

<title>Dropdownlist with Images</title>

<link rel="stylesheet" type="text/css" href="dropdown/dd.css" />

<script type="text/javascript" src="dropdown/js/jquery-1.6.1.min.js"></script>

<script type="text/javascript" src="dropdown/js/jquery.dd.js"></script>

<!-- Script is used to call the JQuery for dropdown -->

<script type="text/javascript" language="javascript">

$(document).ready(function(e) {

try {

    $("#ddlCountryImage").msDropDown();

} catch (e) {

alert(e.message);

}

});

</script>

</head>

<body>

<form id="form1" runat="server">

<table>

<tr>

<td align="right" class="style1">

<b>Country:</b>

</td>

<td>

<asp:DropDownList ID="ddlCountryImage" runat="server" Width="150px"  AutoPostBack="true"></asp:DropDownList>

</td>

</tr>

</table>

</form>

</body>

</html>

In order to demonstrate the process of executing an application from C#, create a new web application project in Visual Studio 2010. Add using statements above the namespace declaration to enable the use of non-fully qualified references to other namespace types.

Now add the following namespace.

using System.Data.SqlClient;

using System.Data;

 

Now write the connection string to connect to the database.

 

string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";

 

In Codebehind write the following code like this. 

Codebehind

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        BindDropDownList();

        Imagetitlebind();  

    }

}

 

protected void Imagetitlebind()

{

    if (ddlCountryImage != null)

    {

        foreach (ListItem li in ddlCountryImage.Items)

        {

            li.Attributes["title"] = "Images/" + li.Value; // Define the folder of image which is defined in the application.

        }

    }

}

 

protected void BindDropDownList()

{

    SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

    con.Open();

    SqlCommand cmd = new SqlCommand("select * from CountryImage", con);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);

    ddlCountryImage.DataTextField = "CountryName";

    ddlCountryImage.DataValueField = "CountryImage";

    ddlCountryImage.DataSource = ds;

    ddlCountryImage.DataBind();

    con.Close();

}

}

 

Now run the application and test how your DropDownList will be:


ddl3.gif

 

Now click on the DropDownList.


ddl4.gif

Some Helpful Resources

How to Bind Country, State, City using Ajax Update Panel

Bind Countries to a DropDownList in ASP.NET

CheckBox, CheckBoxList and DropDownList Controls in Asp.Net

DropDownList with country, state and city in ASP. NET

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rohatash Kumar
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.