ASP.NET - drop downlist with optiongroup  ASP.NET - drop downlist with optiongroup

Asked By Bala Marish
05-May-11 03:16 AM

how to create option group with droupdownlist

please anyone known help me
  Ravi S replied to Bala Marish
05-May-11 03:17 AM
HI

try thsi code

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();
    }
}
  Ravi S replied to Bala Marish
05-May-11 03:18 AM
hi

u can try this

<ddlb:optiongroupselect id="OptionGroupSelect1" runat="server" enableviewstate="true">
   <ddlb:OptionGroupItem ID="OptionGroupItem1" 
      runat="server" Value="1" Text="ONE" OptionGroup="Odd" />
   <ddlb:OptionGroupItem ID="OptionGroupItem2" 
      runat="server" Value="2" Text="TWO" OptionGroup="Even" />
   <ddlb:OptionGroupItem ID="OptionGroupItem3" 
      runat="server" Value="3" Text="TREE" OptionGroup="Odd" />
   <ddlb:OptionGroupItem ID="OptionGroupItem4" 
      runat="server" Value="4" Text="FOUR" OptionGroup="Even" />
</ddlb:optiongroupselect>

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
      this.OptionGroupSelect1.Items.Add(new OptionGroupItem("A", 
                              "Letter A", "Letters"));
      this.OptionGroupSelect1.Items.Add(new OptionGroupItem("B", 
                              "Letter B", "Letters"));
      this.OptionGroupSelect1.Items.Add(new OptionGroupItem("C", 
                              "Letter C", "Letters"));
  }
}

to obtain a select control as shown below:

  Bala Marish replied to Ravi S
05-May-11 03:20 AM
in which place i wrote that


<ddlb:optiongroupselect id="OptionGroupSelect1" runat="server" enableviewstate="true">
   <ddlb:OptionGroupItem ID="OptionGroupItem1" 
      runat="server" Value="1" Text="ONE" OptionGroup="Odd" />
   <ddlb:OptionGroupItem ID="OptionGroupItem2" 
      runat="server" Value="2" Text="TWO" OptionGroup="Even" />
   <ddlb:OptionGroupItem ID="OptionGroupItem3" 
      runat="server" Value="3" Text="TREE" OptionGroup="Odd" />
   <ddlb:OptionGroupItem ID="OptionGroupItem4" 
      runat="server" Value="4" Text="FOUR" OptionGroup="Even" />
</ddlb:optiongroupselect>
  Vickey F replied to Bala Marish
05-May-11 08:54 AM

Hi,

No built-in support <optgroup> in ASP.NET, so you will have to render the tags yourself. Best way to do this is via a control adapter, that enables you to manipulate the outputted (X)HTML of an existing control (in this case the drop down). Could show you an example, but the following article seems to be pretty good:

http://www.codeproject.com/aspnet/DropDownListOptionGroup.asp

Hope this helps,

Create New Account
help
Help me. . . . . . hi all, i just want to say that actually i am new to asp.net 2.0 i have a work over my head and really m not knowing how UserType"] = "Employee" , otherwise store Session["UserType"] = "ReportingOfficer" Check the session value and redirect to appropriate pages. and also store the userid in the session. How i do all this, plz tell all this in details. actually m not knowing anything about all this Post your login page code, we shall start from there <% @ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Login.aspx.cs" Inherits = "_Default" %> <! DOCTYPE html PUBLIC "- / / W3C / / DTD 003300" > < / asp : Label > < / td > < td align = "center" style = "background-color: ActiveBorder; width: 312px;"> &nbsp; < asp : DropDownList ID = "DropDownList1" runat = "server" Style = "z-index: 100; left: 275px; position: absolute; top: 147px" Width ListItem Selected = "True" Text = "User" Value = ""> < / asp : ListItem > < asp : ListItem > Reporting Officer < / asp : ListItem > < / asp : DropDownList > < / td > < tr > < td colspan = '2' style = "background-color:Gray;height: 26px" align = "center"> < asp : Button how to do all the work, so plz help me to run this whole login page. . . <% @ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Login.aspx.cs" Inherits = "_Default" %> <! DOCTYPE html PUBLIC "- / / W3C
use droupdown with optiongroup u got answer here http: / / eggheadcafe.com / community / aspnet / 17 / 10296702 / drop-downlist-with-optiongroup.aspx hi, No built-in support <optgroup> in ASP.NET, so you will have to render the tags yourself. Best way to do this is you to manipulate the outputted (X)HTML of an existing control (in this case the drop down).simplest method would be to apply it to the datasource attribute of the dropdownlist like so: yourdropdownlistid. DataSource = optgroup; yourdropdownlistid. DataBind(); You can also use the DataTextField and DataValueField yourdropdownlistid.DataValueField = "yourfieldnameB" ; yourdropdownlistid.DataBind(); hope this will help you Hi, you can create a drop down with Option Group .JUst look at the code i have given using System; using System.Web.UI.WebControls; using System.Collections.Generic; using System.Web; / / This codes makes the dropdownlist control recognize items with "- -"' / / for the label or items with an OptionGroup attribute and render
Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B A) What is scavenging? (B) What are different types of caching using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are
in Sql server as var_name int How do you separate business logic while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level debugging enable the trace to true in the line in the html format of the page. %@ Page Language = ”vb” trace = ”true”AutoEventWireup = ”false” Codebehind = ”WebForm1.aspx.vb” Inherits = ”WebApplication2.WebForm1?&gt; 2
Migration from ASP to ASP.net How to convert ASP site to ASP.NET site using C# http: / / www.asp.net / downloads / archived-v11 / migration-assistants / asp-to-aspnet hi, ASP.NET framework is very much different from unstrucured ASP and there is no correct way to