Dropdownlist issue

Last post 11-16-2009 11:41 AM by alaa9jo. 19 replies.

Sort Posts:

  • Dropdownlist issue

    11-08-2009, 5:31 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    Hi

    I need to display a table content to dropdowntable. The table is something like this

    Table_1

    Type        SubType

    AirChina   Airlines1

    AirChina   Airlines2


    Now what I need is the dropdownlist should show like this


    AirChina  (As a Heading) followed by sub headlines like this

    Airlines1

    Airlines 2

    With datareader I am able to pull all the details from table, but I don't have any idea how to show AirChina(only once) and below all the subtype under AirChina.

    Please advise me what to do


  • Re: Dropdownlist issue

    11-08-2009, 6:35 AM
    • Participant
      1,600 point Participant
    • jitendramcu
    • Member since 08-28-2009, 8:58 AM
    • Mumbai
    • Posts 279

    For this u have to create a group dropdown list.

    in html 

    <select>
    <optgroup label="Numbers">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    </optgroup>
    <optgroup label="Letters" disabled="true">
    <option value="4">A</option>
    <option value="5">B</option>
    <option value="6">C</option>
    </optgroup>
    </select>

     but in asp.net no server control is available so

    Read this link 

    http://www.codeproject.com/KB/custom-controls/DropDownListOptionGroup.aspx

     

    Thanks,if it helps you then mark as "Answered"
    -----------------------------------------------
    Jitendra Kr.
    http://jitendra-aspnet.blogspot.com/


  • Re: Dropdownlist issue

    11-08-2009, 8:07 AM
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    I know exactly what you need...a group dropdownlist..,I faced exactly the same issue and ended up building my customized dropdownlist,I didn't post it's code till someone in this forums faced this issue too and helped him,here is the link:

    http://forums.asp.net/p/1477378/3438538.aspx

    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
  • Re: Dropdownlist issue

    11-08-2009, 8:28 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    Thank you both of you for the reply. But one question.I need to get the values from database table. So How can I accomplish the code which you provided with my code to retrieve value from database using datareader

  • Re: Dropdownlist issue

    11-08-2009, 8:50 AM
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    cutiepie2008:
    So How can I accomplish the code which you provided with my code to retrieve value from database using datareader

    ahh...which code Undecided? my code or Jitendra's code?

    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
  • Re: Dropdownlist issue

    11-08-2009, 8:59 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    I am referring to  Jitendra's code which looked simple to me. Honestly I didn't understand working of the link you provided. Sorry because of my ignorance . I am just a newbie. So if you could provide a solution to what I have asked I would be happy. Could you please look into the link he provided and provide me a solution to change that according to my requirement. My list items should be dynamic

    As I mentioned in my first post I am using datareader to pull data from sql

    So myreader["Type"] will contain Heading and myreader["SubType"] will contain sub items

    So I need to pull all sub items under that header and display. Could you provide em the solution

  • Re: Dropdownlist issue

    11-08-2009, 9:12 AM
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    Is it OK for you if you fill the data of data reader into a datatable then bind it to a customized dropdownlist that I'm willing to post it's code here?! because It will work fine that way!

    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
  • Re: Dropdownlist issue

    11-08-2009, 9:20 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    It will be okay for me. But could you provide me the code for it?Because I don't know how to do it. I hope code will not be complicated to understand

  • Re: Dropdownlist issue

    11-08-2009, 10:13 AM
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    Ok..Follow my steps:

    1)Create a new project of type ClassLibrary and name it ("GroupDropDownList")

    2)rename the class name from Class1.cs to GroupDropDownList.cs

    3)right-click on your solution then choose "Add Reference" then from ".Net" tab choose System.Web then press OK

    3)replace GroupDropDownList.cs code with this (yes,copy-paste):

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;
    
    namespace GroupDropDownList
    {
        /// <summary>
        /// Summary description for GroupDropDownList.
        /// </summary>
        [ToolboxData("<{0}:GroupDropDownList runat=server></{0}:GroupDropDownList>")]
        public class GroupDropDownList : System.Web.UI.WebControls.DropDownList
        {
            /// <summary>
            /// The field in the datasource which provides values for groups
            /// </summary>
            [DefaultValue(""), Category("Data")]
            public virtual string DataGroupField
            {
                get
                {
                    object obj = this.ViewState["DataGroupField"];
                    if (obj != null)
                    {
                        return (string)obj;
                    }
                    return string.Empty;
                }
                set
                {
                    this.ViewState["DataGroupField"] = value;
                }
            }
            /// <summary>
            /// if a group doesn't has any enabled items,there is no need
            /// to render the group too
            /// </summary>
            /// <param name="groupName"></param>
            /// <returns></returns>
            private bool IsGroupHasEnabledItems(string groupName)
            {
                ListItemCollection items = this.Items;
                ListItem item;
                for (int i = 0; i < items.Count; i++)
                {
                    item = items[i];
                    if (item.Attributes["DataGroupField"].Equals(groupName) && item.Enabled)
                    {
                        return true;
                    }
                }
                return false;
            }
            /// <summary>
            /// Render this control to the output parameter specified.
            /// Based on the source code of the original DropDownList method
            /// </summary>
            /// <param name="output"> The HTML writer to write out to </param>
            protected override void RenderContents(HtmlTextWriter writer)
            {
                ListItemCollection items = this.Items;
                int itemCount = this.Items.Count;
                string curGroup = String.Empty;
                string itemGroup;
                bool bSelected = false;
    
                if (itemCount <= 0)
                {
                    return;
                }
    
                for (int i = 0; i < itemCount; i++)
                {
                    ListItem item = items[i];
                    itemGroup = (string)item.Attributes["DataGroupField"];
                    if (itemGroup != null && itemGroup != curGroup && IsGroupHasEnabledItems(itemGroup))
                    {
                        if (curGroup != String.Empty)
                        {
                            writer.WriteEndTag("optgroup");
                            writer.WriteLine();
                        }
    
                        curGroup = itemGroup;
                        writer.WriteBeginTag("optgroup");
                        writer.WriteAttribute("label", curGroup, true);
                        writer.Write('>');
                        writer.WriteLine();
                    }
                    // we don't want to render disabled items
                    if (item.Enabled)
                    {
                        writer.WriteBeginTag("option");
                        if (item.Selected)
                        {
                            if (bSelected)
                            {
                                throw new HttpException("Cant_Multiselect_In_DropDownList");
                            }
                            bSelected = true;
                            writer.WriteAttribute("selected", "selected", false);
                        }
    
                        writer.WriteAttribute("value", item.Value, true);
                        writer.Write('>');
                        HttpUtility.HtmlEncode(item.Text, writer);
                        writer.WriteEndTag("option");
                        writer.WriteLine();
                    }
                }
                if (curGroup != String.Empty)
                {
                    writer.WriteEndTag("optgroup");
                    writer.WriteLine();
                }
            }
    
            /// <summary>
            /// Perform data binding logic that is associated with the control
            /// </summary>
            /// <param name="e">An EventArgs object that contains the event data</param>
            protected override void OnDataBinding(EventArgs e)
            {
                // Call base method to bind data
                base.OnDataBinding(e);
    
                if (this.DataGroupField == String.Empty)
                {
                    return;
                }
                // For each Item add the attribute "DataGroupField" with value from the datasource
                IEnumerable dataSource = GetResolvedDataSource(this.DataSource, this.DataMember);
                if (dataSource != null)
                {
                    ListItemCollection items = this.Items;
                    int i = 0;
    
                    string groupField = this.DataGroupField;
                    foreach (object obj in dataSource)
                    {
                        string groupFieldValue = DataBinder.GetPropertyValue(obj, groupField, null);
                        ListItem item = items[i];
                        item.Attributes.Add("DataGroupField", groupFieldValue);
                        i++;
                    }
                }
    
            }
    
            /// <summary>
            /// This is copy of the internal ListControl method
            /// </summary>
            /// <param name="dataSource"></param>
            /// <param name="dataMember"></param>
            /// <returns></returns>
            private IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
            {
                if (dataSource != null)
                {
                    IListSource source1 = dataSource as IListSource;
                    if (source1 != null)
                    {
                        IList list1 = source1.GetList();
                        if (!source1.ContainsListCollection)
                        {
                            return list1;
                        }
                        if ((list1 != null) && (list1 is ITypedList))
                        {
                            ITypedList list2 = (ITypedList)list1;
                            PropertyDescriptorCollection collection1 = list2.GetItemProperties(new PropertyDescriptor[0]);
                            if ((collection1 == null) || (collection1.Count == 0))
                            {
                                throw new HttpException("ListSource_Without_DataMembers");
                            }
                            PropertyDescriptor descriptor1 = null;
                            if ((dataMember == null) || (dataMember.Length == 0))
                            {
                                descriptor1 = collection1[0];
                            }
                            else
                            {
                                descriptor1 = collection1.Find(dataMember, true);
                            }
                            if (descriptor1 != null)
                            {
                                object obj1 = list1[0];
                                object obj2 = descriptor1.GetValue(obj1);
                                if ((obj2 != null) && (obj2 is IEnumerable))
                                {
                                    return (IEnumerable)obj2;
                                }
                            }
                            throw new HttpException("ListSource_Missing_DataMember");
                        }
                    }
                    if (dataSource is IEnumerable)
                    {
                        return (IEnumerable)dataSource;
                    }
                }
                return null;
            }
            #region Internal behaviour
            /// Saves the state of the view.
            /// </summary>
            protected override object SaveViewState()
            {
                // Create an object array with one element for the CheckBoxList's
                // ViewState contents, and one element for each ListItem in skmCheckBoxList
                object[] state = new object[this.Items.Count + 1];
    
                object baseState = base.SaveViewState();
                state[0] = baseState;
    
                // Now, see if we even need to save the view state
                bool itemHasAttributes = false;
                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (this.Items[i].Attributes.Count > 0)
                    {
                        itemHasAttributes = true;
    
                        // Create an array of the item's Attribute's keys and values
                        object[] attribKV = new object[this.Items[i].Attributes.Count * 2];
                        int k = 0;
                        foreach (string key in this.Items[i].Attributes.Keys)
                        {
                            attribKV[k++] = key;
                            attribKV[k++] = this.Items[i].Attributes[key];
                        }
    
                        state[i + 1] = attribKV;
                    }
                }
    
                // return either baseState or state, depending on whether or not
                // any ListItems had attributes
                if (itemHasAttributes)
                    return state;
                else
                    return baseState;
            }
    
            /// <summary>
            /// Loads the state of the view.
            /// </summary>
            /// <param name="savedState">State of the saved.</param>
            protected override void LoadViewState(object savedState)
            {
                if (savedState == null) return;
    
                // see if savedState is an object or object array
                if (savedState is object[])
                {
                    // we have an array of items with attributes
                    object[] state = (object[])savedState;
                    base.LoadViewState(state[0]);   // load the base state
    
                    for (int i = 1; i < state.Length; i++)
                    {
                        if (state[i] != null)
                        {
                            // Load back in the attributes
                            object[] attribKV = (object[])state[i];
                            for (int k = 0; k < attribKV.Length; k += 2)
                                this.Items[i - 1].Attributes.Add(attribKV[k].ToString(),
                                                               attribKV[k + 1].ToString());
                        }
                    }
                }
                else
                    // we have just the base state
                    base.LoadViewState(savedState);
            }
            #endregion
        }
    }
    

    4) Rebuild the solution.

    5) Open the solution that has your website

    6) in the toolbox,right-click then choose "Add tab",then right the name of the tab...let name it "Group DropDownList"

    7) Right-click on that tab then select "choose items",then from ".Net FrameWork Components" click "Browse",then point to the dll that was created by our class library..it's name is "GroupDropDownList.dll".then click OK,then OK for the other dialog,you will notice that out group dropdownlist control has been added to the toolbox

    8) drag-n-drop

    9) horray..the control is in our page,what to do next? use it!!,but how? do it like this in Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //This is your data,right?
    
                //Type        SubType
                //AirChina   Airlines1
                //AirChina   Airlines2
    
                //This block is for demo only
                DataTable table1 = new DataTable();
                table1.Columns.Add("Type");
                table1.Columns.Add("SubType");
    
                DataRow mydatarow = table1.NewRow();
                mydatarow["Type"] = "AirChina";
                mydatarow["SubType"] = "Airlines1";
                table1.Rows.Add(mydatarow);
    
                mydatarow = table1.NewRow();
                mydatarow["Type"] = "AirChina";
                mydatarow["SubType"] = "Airlines2";
                table1.Rows.Add(mydatarow);
                //
    
                //Very Important Note:your data must be sorted by "Type"
                //since it contains our groups
                DataView myview = table1.DefaultView;
                myview.Sort = "Type asc";
    
                GroupDropDownList1.DataTextField = "SubType";
                //DataValueField usually must be the column that has the id of subType
                //so there must be a third column
                //but since this is a demo,I will assume that the value and text are the same
                GroupDropDownList1.DataValueField = "SubType";
    
                GroupDropDownList1.DataGroupField = "Type";
                GroupDropDownList1.DataSource = myview;
                GroupDropDownList1.DataBind();
            }
        }


    This will work 100%!!

    maybe my code in the class library is a little advanced but I made some comments on it

    as best as I could,you can see it's very easy to use it!

    If you need help just let me know ok Cool



    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
  • Re: Dropdownlist issue

    11-08-2009, 10:20 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    Thanks for your code. I will try this and will revert back to you

  • Re: Dropdownlist issue

    11-08-2009, 10:44 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    Hi


    I created a project and in the groupdropdownnlist.cs file I copied all the code you have given. Also I have referenced System.web namespace. But whrn I rebuild teh solution I got the following errors

    Error 11 The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) C:\DEEPA\Dotnet Projects\Online Reservation\OnlineReservation\GroupDropdownlist\GroupDropdownlist.cs 12 18 GroupDropdownlist
    Error 11 The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) C:\DEEPA\Dotnet Projects\Online Reservation\OnlineReservation\GroupDropdownlist\GroupDropdownlist.cs 12 18 GroupDropdownlist

    Error 14 The type or namespace name 'HtmlTextWriter' could not be found (are you missing a using directive or an assembly reference?) C:\DEEPA\Dotnet Projects\Online Reservation\OnlineReservation\GroupDropdownlist\GroupDropdownlist.cs 69 48 GroupDropdownlist

    Error 15 The type or namespace name 'ToolboxData' could not be found (are you missing a using directive or an assembly reference?) C:\DEEPA\Dotnet Projects\Online Reservation\OnlineReservation\GroupDropdownlist\GroupDropdownlist.cs 21 6 GroupDropdownlist

    Error 16 The type or namespace name 'ToolboxDataAttribute' could not be found (are you missing a using directive or an assembly reference?) C:\DEEPA\Dotnet Projects\Online Reservation\OnlineReservation\GroupDropdownlist\GroupDropdownlist.cs 21 6 GroupDropdownlist

    Any ideas


  • Re: Dropdownlist issue

    11-08-2009, 10:56 AM
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    I don't think that System.Web is referenced,please make sure that it's referenced.

    Tip: in references folder,you must see those dlls:

    System

    System.Data

    System.Web

    System.Xml

    then rebuild your solution.


    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
  • Re: Dropdownlist issue

    11-08-2009, 10:59 AM
    • Member
      155 point Member
    • cutiepie2008
    • Member since 12-22-2008, 9:25 PM
    • United States
    • Posts 141

    Hi


    It worked like a charm.Only one issue I have. In your code given below

    DataRow mydatarow = table1.NewRow();
                mydatarow["Type"] = "AirChina";
                mydatarow["SubType"] = "Airlines1";
                table1.Rows.Add(mydatarow);
                mydatarow = table1.NewRow();
                mydatarow["Type"] = "AirChina";
                mydatarow["SubType"] = "Airlines2";
                table1.Rows.Add(mydatarow);

    DataRow mydatarow = table1.NewRow();

                mydatarow["Type"] = "AirChina";

                mydatarow["SubType"] = "Airlines1";

                table1.Rows.Add(mydatarow);


                mydatarow = table1.NewRow();

                mydatarow["Type"] = "AirChina";

                mydatarow["SubType"] = "Airlines2";

                table1.Rows.Add(mydatarow);


    Here you are manually giving Type as "airchina" and subtype as "airline1". How can I know what are the values coming from sql? that mean I should give the value from database not manually. Any ideas? Please don't mind if I am talking foolishness. I am in the learning stage still 

  • Re: Dropdownlist issue

    11-08-2009, 11:14 AM
    • All-Star
      26,376 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,761

    @alaa9jo

    that is some pretty cool code! thanks!

    I will definitely try it later. as far as I remember there were at least two other threads where this was wanted. you should post this as an article somewhere (on this site as well as others like codeproject.com etc)

    Regards,
    Peter
  • Re: Dropdownlist issue

    11-08-2009, 11:28 AM
    Answer
    • Contributor
      6,243 point Contributor
    • alaa9jo
    • Member since 07-01-2009, 9:17 AM
    • Jordan
    • Posts 1,144

    Thanks Petenet,but I don't have a blog and don't know how to create one...I wish I had one..I will post many cool articles like that one.Laughing

    Anyway,I will help my friend here in that issue and may be you can help me on how to create a blog (if you know of course)

    My friend,you need to fill the data of datareader into a datatable so you will need a code like this:

            DataTable table1 = new DataTable();
            table1.Columns.Add("Type");
            table1.Columns.Add("SubType");
    
            while (yourdatareader.Read())
            {
                DataRow mydatarow = table1.NewRow();
                mydatarow["Type"] = yourdatareader["Type"].ToString();
                mydatarow["SubType"] = yourdatareader["SubType"].ToString();
                table1.Rows.Add(mydatarow);
            }


    Best Regards,
    Ala'a Alnajjar
    ----------------------------------------------------
    Please remember to click “Mark as Answer” on the post(s) that help(s) you even if they were for the same person.
    Also remember to close the thread by selecting “Resolved”,you will find it in your first post.

    Convert C# to VB.net and visa versus

    My Webblog
Page 1 of 2 (20 items) 1 2 Next >