Gives solution for any Error occurs in any Programming Language, .Net Core 3.1, 3.0, C#,Asp.Net,Sqlserver,MVC,Java,Php,Html,Css,Jquery,errors and solutions,Latest News,Technology

Wednesday, 24 February 2016

Category and Sub Category in Single Dropdown list ASP.NET C#

No comments :

This post explained about Main Category and Sub Category into Single dropdown list using c# asp.net and bootstrap.
Here first you need write below code into App_Code folder. If you have this folder just create class and paste this code otherwise you need to create App_Code folder.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;

public class DropDownListAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            DropDownList list = this.Control as DropDownList;

            string currentOptionGroup;
            List<string> renderedOptionGroups = new List<string>();

            foreach (ListItem item in list.Items)
            {
                if (item.Attributes["OptionGroup"] == null)
                {
                    RenderListItem(item, writer);
                }
                else
                {
                    currentOptionGroup = item.Attributes["OptionGroup"];

                    if (renderedOptionGroups.Contains(currentOptionGroup))
                    {
                        RenderListItem(item, writer);
                    }
                    else
                    {
                        if (renderedOptionGroups.Count > 0)
                        {
                            RenderOptionGroupEndTag(writer);
                        }

                        RenderOptionGroupBeginTag(currentOptionGroup, writer);
                        renderedOptionGroups.Add(currentOptionGroup);

                        RenderListItem(item, writer);
                    }
                }
            }

            if (renderedOptionGroups.Count > 0)
            {
                RenderOptionGroupEndTag(writer);
            }
        }

        private void RenderOptionGroupBeginTag(string name, HtmlTextWriter writer)
        {
            writer.WriteBeginTag("optgroup");
            writer.WriteAttribute("label", name);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteLine();
        }

        private void RenderOptionGroupEndTag(HtmlTextWriter writer)
        {
            writer.WriteEndTag("optgroup");
            writer.WriteLine();
        }

        private void RenderListItem(ListItem item, HtmlTextWriter writer)
        {
            writer.WriteBeginTag("option");
            writer.WriteAttribute("value", item.Value, true);

            if (item.Selected)
            {
                writer.WriteAttribute("selected", "selected", false);
            }

            foreach (string key in item.Attributes.Keys)
            {
                writer.WriteAttribute(key, item.Attributes[key]);
            }

            writer.Write(HtmlTextWriter.TagRightChar);
            HttpUtility.HtmlEncode(item.Text, writer);
            writer.WriteEndTag("option");
            writer.WriteLine();
        }
    }
And write below code into App_Browsers folder. Here you need to save this file with .browser format
<!--
    You can find existing browser definitions at
    <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers
-->
<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.DropDownList"
      adapterType="DropDownListAdapter" />
    </controlAdapters>
  </browser>
</browsers>

And finally write this code in aspx.cs page

ListItem item1 = new ListItem("General Sales", "1");
            item1.Attributes["OptionGroup"] = "Sales";

            ListItem item2 = new ListItem("Payment Issues", "2");
            item2.Attributes["OptionGroup"] = "Sales";

            ListItem item3 = new ListItem("Bug Reports", "3");
            item3.Attributes["OptionGroup"] = "Technical";

            ListItem item4 = new ListItem("Hardware Issues", "4");
            item4.Attributes["OptionGroup"] = "Technical";

            ddlList.Items.Add(item1);
            ddlList.Items.Add(item2);
            ddlList.Items.Add(item3);
            ddlList.Items.Add(item4);

No comments :

Post a Comment