Multiple Column Drop-Down Box for ASP.NET
 
Published: 02 Nov 2004
Unedited - Community Contributed
Abstract
There are many drop-down controls out there to use in Web pages, but I have not seen any drop-down controls that can show multiple columns like in VB or MS Access. I thought of building my own control that will display the columns based on the selection made in the SQL statement (e.g. Select col1, col2, col3 from db). The control is then populated with the data.
by Jayarajan S Kulaindevelu
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53139/ 37

Building the Control

[ Download Code ]

ScreenShot

Screen Shot

How the Control was Built

This is not just one control; it's a composite control, which means it's built using more than one control, resulting in one super control. The figure below demonstrates how the control works. The controls used are a Textbox, Label, DataGrid, and a Panel control that holds all the controls. I have also set the ID property for the TextBox and the DataGrid as "txt" and "DDL," because, due to the implementations of the  INamingContainer interface (to maintain the unique name for the controls), the control would have generated ":CTL1" if I did not set the ID value. The sample picture shows the structure of the control:

Layout

Using the Code

It's very simple to use the code.  I have shown this sample in VB.NET, but you could use it in C# also (download the dll file).  For this control, just add the control to your toolbox by selecting the dll from the location where you unzipped the file. Once you dropped the control in your ASPX page, you could set the properties either through properties window or from the HTML page.

 

 

 

 

Properties & Errors Handled

Download Code
Properties for the Control

There are many properties for this control that are self explainatory, but there are some properties that need some explanation:

  • GridRowColor - This sets the row color of the grid when the mouse is moved on the grid; by default the color is LightGray.
  • GridTextColor - This sets the color of the text in the grid when the mouse is moved on the grid; by default the color is Red.
  • ViewColID - This is one of the cool features for this control.  Imagine your "Select" statement or any other means of populating the grid has two or more columns, and you need to show the 2nd, 3rd, or 4th column in the text display.  You set this property value to show the column you like (the default is 0, which means the first column will be displayed).  If you need to show the 3rd column, set this property value to 2. If you set this value to more than the count of columns, an error will be displayed as shown in the figure below:

Error

 

Based on the value you selected for this property, when you move the mouse over the Grid, the values in the text change.

  • CssClass - Copy this style and paste it in your ASPX page in the HEAD section:

    <style lang="text/css" media="screen">
    .general 

    FONT-SIZE: 8pt; 
    COLOR: navy; 
    FONT-FAMILY: Verdana, Arial, helvetica, sans-serif 
    }
    </style>
  • Height - Do not set the height property in the property window or in your HTML page.  Set this property in your code behind page, e.g.,    MultiColDD_List1.Height = Unit.Pixel(200).
  • ToolTip - The ToolTip value can be changed to any text.

 

Using the Control

Download Code

One more thing, there is one method for this control, "retTotRows."  You have to use this method; otherwise, the control may not work properly.  Actually, this method returns the count of rows to be populated in the drop-down list and generates the click event to be populated in the textbox.

To use this method:

MultiColDD_List1.retTotRows(DS_P.Tables(0).Rows.Count)
//Where DS_P is the dataset ...see code below!

or if you are generating the items from any other means, make sure you pass the total rows to this method.

</font>


<font face="Courier New"><font size="2">Private Sub Page_Load(ByVal sender As System.Object, 
   ByVal e As System.EventArgs) Handles MyBase.Load
   If Not IsPostBack Then

    MultiColDD_List1.Width = Unit.Pixel(300)
    MultiColDD_List1.Height = Unit.Pixel(200) ' make sure you set 
     'the height property, else there will be no scroll bar.
    MultiColDD_List1.DataSource = CreateDataSources() 
     ' this function returns the dataset to bind to the control
    MultiColDD_List1.DataBind()

   End If

  End Sub
   
  Function CreateDataSources() As DataSet
    Dim DS_P As DataSet
    Dim cn As SqlConnection

    Dim cmdP As SqlDataAdapter

    Dim sqlStr As String = 
     "select au_Lname as 'Last Name',au_fname as 'First Name',Phone from authors " '  you could add more cols

    'Change the SQL Server name {Data Source = <your server name>}
    cn = New SqlConnection("Integrated Security=SSPI;Persist Security 
     Info=False;Initial Catalog=Pubs;Data Source=(local)")
    cmdP = New SqlDataAdapter(sqlStr, cn)

    DS_P = New DataSet

    cmdP.Fill(DS_P, "authors")

    MultiColDD_List1.retTotRows(DS_P.Tables(0).Rows.Count) 
     ' make sure you use this method

    Return DS_P


    DS_P.Dispose()
    cmdP.Dispose()
    cn.Close()

  End Function

Retrieving Control Value

Once you select the item, the value for the drop down could be retrieved either using Javascript to send it to another page or directly in the code behind.
Suppose we have an HTML button.  In the Click event handler of this button, the value of the drop down could be sent to another page; the code below demonstrates this:

</font></pre><pre><font face="Verdana" size="2">  <script language="javascript">
  
   function Show(){
    window.open("anotherPage.aspx?ddValue=" + document.getElementById('MultiColDD_List1_txt').value);
   }
  </script>
  <input id="Shows" onclick="Show();" type="button" value="HTML BUTTON">

Make sure that the name of the control ends with "_txt."  To use it in the same page, directly call the control's value like: MultiColDD_List1.Text.

Making Intellisense Work

Download Code

Using the Intellisense

There are many custom control that, when you use them in your ASPX page, don't show the properties for the control, which makes it difficult to find the properties for the control. So how do you make the Intellisense work?  First, you have to generate the schema file for this control, i.e., the XSD.  The zip file contains the XSD for this control. Copy this to the following folder:

  • C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml

and in the BODY tag of your ASPX page add the following code:
<body xmlns:ksj1="urn:http://schemas.ksjControls.com/ASPNET" MS_POSITIONING="GridLayout">

The figure below shows the intellisense.

Intellisense

Features

This control implements the INamingContainer interface.  If you see the HTML output of your ASP.NET pages, the client-side names of the controls don't always match the names you've given them in the server code. This is because ASP.NET automatically renames them to keep things organized and to ensure that no two controls have the same client-side ID.

By implementing the INamingContainer interface, the custom control is identified as a container for the child controls. It also ensures that all the sub-controls are named similarly in the output. This will be important for referencing the controls in client-side code. That's why when you click the show button, the value of the control is taken from MultiColDD_List1_txt.value and not from the ID you have given in the ASPX page, i.e., MultiColDD_List1. Because this is a composite control, instead of using the Render method, I have overridden the CreateChildControls method, and all the tricks are done there. Another feature of this control is that when you move the mouse over the grid, you can see the text changing in the textbox.

Conclusion

Download Code

This control implements the INamingContainer interface. If you see the HTML output of your ASP.NET pages, the client-side names of the controls don't always match the names you've given them in the server code. This is because ASP.NET automatically renames them to keep things organized, and to ensure that no two controls have the same client-side ID.

By implementing the INamingContainer interface, the custom control is identified as a container for the child controls. It also ensures that all the sub-controls are named similarly in the output. This will be important for referencing the controls in client-side code. That's why when you click the show button, the value of the control is take from MultiColDD_List1_txt.value and not from the ID you have given in the ASPX page, i.e., MultiColDD_List1. Because this is a composite control, instead of using the render method, I have overridden the CreateChildControls method, and all the tricks are done here. Another feature of this control is that when you move the mouse over the grid, you can see the text changing in the textbox.



User Comments

Title: ASP.net Multi columns in drop down list   
Name: NaingNaing
Date: 2012-09-16 2:30:42 PM
Comment:
Hello,
Your source code is not ok.So,please give me another asp.net source code like this design.
Thanks
Title: Multiple column into drop down list   
Name: naing
Date: 2012-05-04 2:09:29 AM
Comment:
i want to get code for adding muliple columns into drop down list.So Please send me(asp.net)
Title: Asp.net   
Name: Rajnish
Date: 2011-11-10 3:08:58 AM
Comment:
i want multiplay two clum of table and show on frent end
Title: Good Article   
Name: Ayushi Rastogi
Date: 2010-12-15 7:05:36 AM
Comment:
This is very gud article it gives a solution of my problem.
Thanks you.
Title: Not showing Result   
Name: Sanjay
Date: 2010-10-27 3:01:36 AM
Comment:
No error is coming. But data not disply, if I count the Item after bind they show the number (like 10) is there. But nothing display, Please guide.
Title: Not showing Result   
Name: Sanjay
Date: 2010-10-27 3:00:29 AM
Comment:
I used this control with follow the steps. No error is coming. But data not disply, if I count the Item after bind they show the number (like 10) is there. But nothing display, Please guide.
Title: Not Working   
Name: meliverouz
Date: 2010-04-12 9:29:47 PM
Comment:
the same result with ramesh. follow the steps but not working. debug in ff. and only can see the number 6. please help us. thanks.
Title: An error has occurred in script on this page   
Name: sunis.sun
Date: 2009-09-25 10:37:16 PM
Comment:
"An error has occurred in script on this page" when I view page
Title: cant see the result on page   
Name: ramesh
Date: 2008-11-08 7:16:08 AM
Comment:
I have followed all directions as suggested.

But could not see expect expected results on page.

Can just see rectangle image on screen. and number 6.

if i see page source then everything is there.
Title: Not Working!!   
Name: Guest
Date: 2008-11-07 7:58:18 PM
Comment:
Hi Guys,

Today I am trying to use this control using VS2008. Unfortunately I am getting Empty Dropdown.

The Code have tried:

DataView dv = new DataView();
dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

DataSet ds = new DataSet();
ds.Tables.Add((DataTable)dv.Table.Copy());

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

multiColDD_List1.Width = Unit.Pixel(300);
multiColDD_List1.Height = Unit.Pixel(200);
multiColDD_List1.retTotRows(ds.Tables[0].Rows.Count);
multiColDD_List1.DataSource = ds;
multiColDD_List1.DataBind();
Title: Can i get for code C# and asp.net 2.0?   
Name: Abhishek
Date: 2008-08-13 2:26:46 PM
Comment:
Hi, i am new to web application and i want to use this functionality in my web application...So can i get the code for asp.net 2.0 and visual studio 2005???

Thanks
Title: mutlticode is error   
Name: jj
Date: 2008-08-07 3:46:34 AM
Comment:
hi this project is not working properly........
please correct way of the project
Title: MultiColDD_List1_DDL not defined error   
Name: rahul
Date: 2008-07-30 9:35:46 AM
Comment:
hi
i am tryin to use your control but its comin empty...and if click the error tag down it complains about line 58 i.e

"MultiColDD_List1_DDL not defined"
can you plz urgently reply with solution on memafiaso@gmail.com
Title: need code   
Name: hariprasad
Date: 2008-03-31 1:23:13 AM
Comment:
i want to code for wich effocts are there in above in this url
http://aspalliance.com/541_Multiple_Column_DropDown_Box_for_ASPNET
Title: Need your help   
Name: palash chatterjee
Date: 2008-03-14 2:11:01 AM
Comment:
I am trying to use your controll, but it is not working, though I followed your each and every steps rightly.Please note that,I am using C# with Asp.Net 2.0 and SqlServer 2000 version.
When it runs, it simply shows only textbox and drop down icon in the browser.
If possible, please guide me in this regard.

Thanks & Regards

Palash Chaterjee
Title: Mr   
Name: Jamal
Date: 2008-01-16 1:24:09 PM
Comment:
Havent been able to get this working in my express edition of Web Developer. Can you please contact me at jsimmt98@yahoo.com
Title: Hi   
Name: Azeef
Date: 2007-11-05 5:08:46 AM
Comment:
Hi I got java script error while click the drop down button.Please help and what to do ??

if (document.getElementById(ddL).style.visibility == 'visible') {
mail ID :
azecse@rediffmail.com
Title: Datasource property is not valid   
Name: anil
Date: 2007-10-18 6:41:35 AM
Comment:
Hi Jayarajan S Kulaindevelu, i tries to use this control but its not working in vs2005. i am getting problem in datasource property. please help me. please send me @ mathi46@rediffmail.com
Title: Can not use in .net   
Name: bhavesh
Date: 2007-08-06 4:11:44 PM
Comment:
Hi,
I tried to use it in vs 2005 and it is not working. I am getting data from database but when i bind it with dropdownlist it does not show any items. when i click it shows error in line 56.
Can you help ?
Thanx
Title: HELP PLEASE   
Name: Dalik
Date: 2007-07-04 10:01:55 AM
Comment:
I have a C# web application and i have apply everything you say, but when i run the code then i dont see anything. The control is there but when i click i get nothing, it doesnt even open. I am using master page, do you think that that has something to do??????Please HELP ME dalik_81@yahoo.com
Title: i cant get soluction   
Name: srs
Date: 2007-04-27 1:54:03 AM
Comment:
i have doubt that it possible we directly add the text ie value in drop down list in run time.
it possible we type text in drop downlist..
Title: plz help   
Name: Priya
Date: 2007-04-09 3:21:17 AM
Comment:
when i m using this control,getting javascript error(line :-if (document.getElementById(ddL).style.visibility == 'visible')) in the function 'ShowhideDD' when clicking arrow.
Title: help needed   
Name: yumna
Date: 2006-12-10 6:37:40 AM
Comment:
Hi,
I used ur multicolumn dropdownlist and its amazing.But I hv a small problem,I need to hide one of the column that I hv bound to the dropdownlist.In short ,I have the same problem as sameer's.Could you plz mail me the new dll also

Thanks in advance
Title: Great Work   
Name: Bogdan Prodan
Date: 2006-08-31 8:43:16 AM
Comment:
Nice control, but i didn't succed to use it in my C# aplication. I started it in your example and looks great, works fine, but when i put it in my aplication i receive




CS0234: The type or namespace name 'multiColDD_List' does not exist in the class or namespace 'multiColDD_List.multiColDD_List' (are you missing an assembly reference?)


private System.Web.UI.Control __BuildControlMultiColDD_List1() {
Line 81: multiColDD_List.multiColDD_List __ctrl;

Could u help me? Thx in advance
Title: Excellent Work   
Name: Shekhar
Date: 2006-06-13 2:54:51 PM
Comment:
Ur code is excellent and it works as per expected
But, it would be nice if the functionality of search on text box is included as per suggested by Anil like a web DropDown
Please send me the new dll if you have already worked on this or suggest me what to do
My E-MailId is shekhar_lko23@yahoo.co.in

Thanks
Title: searching option in drop down   
Name: Anil
Date: 2006-06-08 7:30:34 AM
Comment:
Hi,

i tried your control , its simply good i want little bit extends to this functionality like i want to give search keys on text change , when i enter text in textbox corresponding matched item should focused with color.
if possible plz mail me at : naaniau@inbox.com
Title: SIMILAR CODE IN C#   
Name: alex
Date: 2006-04-07 4:51:11 AM
Comment:
could u please upload the same code in C#
Title: fortihacker   
Name: JAPHspam
Date: 2006-03-16 10:05:27 PM
Comment:
email:ca920105@gmail.com I just like spam! I'm collocting junk email...
Title: Does it work with ASP 2.0 and Visual Studio 2005?   
Name: Sarah Bendele
Date: 2006-01-18 1:28:43 PM
Comment:
I cannot get this code to work in visual studio 2005. There they are no longer using data adapters, rather data sources, and it's quite a bit different than 2003. Neither of which I am an expert in yet. Any help you can offer in this area is greatly appreciated.
Title: Urgent Help needed on Multi Column Combo   
Name: Reply to Sameer
Date: 2005-09-15 9:31:50 AM
Comment:
Hi Sameer,

Could you send me your email address to jayshu_01@yahoo.com, I will send you the new dll

Thanks
Title: Urgent Help needed on Multi Column Combo   
Name: Sameer
Date: 2005-09-15 8:03:01 AM
Comment:
Hi,
I tried with the multiColDD_List.dll for achiving the functionality of Multi Column Drop Down.
My Drop Down need to get bound with the Customer_ID,Customer_Name,Customer_Address.
If I am assigning the datatable to the data source of the Combo.The Drop Down is showing all the Column in the datatable.I want to hide the Column Customer_ID.
I am setting the

MultiColDD_List1.ViewColID = 1

This will Show me the Customer_Name in the Text Box of the Combo.But While saving this data I need the Customer_Id of the selected Customer from the Drop Down.
1.How should I get the Customer_ID of the selected Customer?
2.How to Hide the requird column in the drop down.

Please guide me on this.

Thanx And Regards
Sameer
Title: Question   
Name: Jason
Date: 2005-02-14 5:10:45 PM
Comment:
Is your source code for the control available somewhere? I would like to see how you accomplished the building of the control.

Thanks,
Title: Similar Code in C#   
Name: Eran
Date: 2004-12-15 5:55:33 AM
Comment:
Could you please upload similar code in C# ?
Thank you !
Title: Good Article   
Name: sameer lal
Date: 2004-11-17 8:33:11 AM
Comment:
Good stuff.Keep it up.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 10:11:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search