Asp.Net Server control showing groups in Drop Down list
Hi
The control has some new argument over the dropdown list. There is a new property called DataGroupField. When we are binding the data then we need to asign the value of the group field. Also the data need to be sorted on the group itself otherwise the same group will be show twice (I have not added sorting of the data based on the group in the control as yet. But waiting to see if this will be required.).
You can also add the items one by one. In this case you need to provide the group of the item also. The add methods take and object of type OptionListItem. The optionListItem takes three argument, ItemName, Value and the group. (I know you will have the question why not the type ListItem. The reason is ListItme type can track only the name and value. To add the group I had to create a new type.)
The control also keeps track of its items (which also includes the group.). 
To use the control download the provided dll. Add the reference to the dll in the project. Now you need to register the control in the page. 
<%@ Register Assembly="OptionCtrl" Namespace="VikServerControl" TagPrefix="cc1" %>
You can add the control to the page with the following directive. 
<cc1:OptionDropDownList id="OptionDropDownList1" runat="server"><//cc1:OptionDropDownList>        
Now you can add the items in the code behind (The support for adding the items in HTML designer is not there as yet.). In the code behind we can either add the items one by one using the Items.Add method. 
 OptionDropDownList1.Items.Add(new VikServerControl.OptionListItem(dt.Rows[i]["Text"].ToString(), dt.Rows[i]["Value"].ToString(), dt.Rows[i]["Group"].ToString()));
Or we can databind the Items to the control. If you are using the databind method do not forget to provide the DataGroupField value. DataGroupField will the the column name for the data of the Group. if the value for the DataGroupField is not provided then the value of the text is taken as the Group name.
OptionDropDownList1.DataGroupField = "CategoryName";
OptionDropDownList1.DataTextField = "ProductName";
OptionDropDownList1.DataValueField = "ProductID";
OptionDropDownList1.DataSource = ds.Tables[0];
OptionDropDownList1.DataBind();

You can also get the selectedIndexChanged event for the control. This works same as the dropdown list or Listbox. The list box control for the same is alos available for the same. The listbox control allows multiple selection also (as usual controlled by the property SelectionMode).

Here is the Link to the download of the dll Download

Thanks
VikramP.S. Do not hesitate to use the control and give any feed back.

Feedback

Posted on 7/18/2007 10:20:00 AM

Could you tell me where i can find the dll for this control?

Posted on 7/18/2007 1:00:06 PM

Where is the control? IT looks awesome but can't find it anywhere.

Posted on 7/18/2007 7:10:13 PM

Hi Carlos,

Sorry for the mistake. I have updated the post to contain the link also. :)

Posted on 7/19/2007 5:31:12 AM

thanks man.Great work by the way... very good work!

Posted on 7/19/2007 1:36:37 PM

great simple to use control...one question, is there a way to indent the listings under each group?

Posted on 7/19/2007 6:13:28 PM

Hi,

Currently there is no way to provide the indentation. Although the optiongrp and option can be differentiated easily. I will check if that can be provided in the future version

Posted on 8/20/2007 1:31:21 PM

The way that you would get the options to indent is by wrapping them within the optgroup tag. Since you just create and close the optgroup tag, it doesn't actually indent the items, since they aren't technically a part of any group.

By the way, it would be great if you could include the source code along with the DLL. I'd have made these changes for you, but no source. :)

Posted on 8/20/2007 2:03:17 PM

I apologize if the last post was duped, but I wasn't sure if anything went through or if you moderate posts (there is no message, so it is not clear).

Anyway, I hope this post goes through. I used .NET Reflector to view your code, and here is the changes needed to fix the optiongroup indent:

protected override void RenderContents(HtmlTextWriter writer) {
            OptionListItemCollection items = this.Items;
            int count = this.Items.Count;
            bool flag = false;
            string group = "";
            if (count > 0) {
                for (int i = 0; i < count; i++) {
                    OptionListItem item = items[i];
                    if (group != item.Group && i > 0) {
                        writer.WriteEndTag("optgroup");
                        writer.WriteLine();
                    }
                    if (group != item.Group) {
                        writer.WriteBeginTag("optgroup");
                        writer.WriteAttribute("label", item.Group, true);
                        writer.Write('>');
                        group = item.Group;
                    }
                    writer.WriteBeginTag("option");
                    if (item.Selected) {
                        if (flag) {
                            throw new HttpException("Cant_Multiselect_In_DropDownList");
                        }
                        flag = true;
                        writer.WriteAttribute("selected", "selected", false);
                    }
                    writer.WriteAttribute("value", item.Value, true);
                    writer.Write('>');
                    HttpUtility.HtmlEncode(item.Text, writer);
                    writer.WriteEndTag("option");
                    writer.WriteLine();
                }
                writer.WriteEndTag("optgroup");
                writer.WriteLine();
            }
        }


                
Posted on 8/20/2007 6:00:41 PM

HI Tim,

Thanks for the changes in the code. I will look into it and post the new version soon.

P.S. I will also check on the message for the comments and fix it.

Posted on 8/21/2007 7:17:30 PM

Hi Tim,

I have updated the design to show that the comment has been entered for moderation at the Top. Hope this will help

Posted on 8/24/2007 8:13:33 AM

Hi Tim and All,

I have updated the control and the Latest version is available for download at here

Posted on 3/13/2008 4:24:36 AM

Hi Vikram,
it is really nice to see the control like this, very helpful, i am very new to .NET and specially c-sharp, i need one thing urgently done, which i have looked and try to sort out from your code, but unable to do that.

I need to have different colours for the groups, is there any possibility to do it, please can you help me out on this?

Posted on 7/17/2008 3:12:11 AM

how can i access the groupfield text on java script.

Posted on 12/22/2009 2:55:18 PM
Posted on 6/3/2010 5:11:47 PM

i am using this dll, but there are some secury issues are coming when i uplod this with asp.net 3.5 & IIS 7.

The Error i am getting :

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Please see if you can help me.

Posted on 6/4/2010 11:00:48 AM

can we add color to the text in the dropdownlist.

Posted on 9/21/2010 10:20:53 PM

It would be nice if I could add the group count to the group title. This would let me see how many items are in each section.

Posted on 1/19/2011 12:34:05 PM

Thanks Sir, You Have Done A Great Work, It Perfectly Works As Per My Requirement.
Good Work !

Posted on 3/1/2011 3:50:39 AM

As mentioned by Tim earlier the optgroup doesn't indent the items properly. If you could update us with the latest dll for this control would be really helpul and appreciated.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia