Thursday, October 26, 2017

Dropdownlist with optgroup using WebControlAdapters

I need to do a dropdownlist with optgroup. I found lots of guides and all foresee the use of WebControlAdapter this is the guide that I'm fllowing
I've added the class to my project:
namespace admin
{
  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();
    }
  }
}
I've added the folder App_Browser to my project (did not exist) and I've added the file BrowserFile.browser
<browsers>
  <browser refID="IE6to9">
    <controlAdapters>
       <adapter controlType="System.Web.UI.WebControls.DropDownList"
  adapterType="admin.DropDownListAdapter" />
    </controlAdapters>
  </browser>
</browsers>
in my page .aspx (that is in the same folder of the class DropDownListAdapter I have
<asp:DropDownList runat="server" ID="ddlModelli" CssClass="form-control multipleSelect"></asp:DropDownList> 
that is filled in this way
public static void loadDDLModelli(ref DropDownList ddl, List<dynamic> objects)
    {
        Int16 cont = 0;
        ddl.Items.Clear();
        System.Web.UI.WebControls.ListItem li;
        String idModello = "";
        String nomeModello = "";
        String nomeBrand = "";
        String oggetto = "";

        foreach (var item in objects)
        {
            oggetto = item.ToString().Replace("{", "").Replace("}", "");
            idModello = oggetto.Split(',')[0].Split('=')[1].Trim();
            nomeModello = oggetto.Split(',')[1].Split('=')[1].Trim();
            nomeBrand = oggetto.Split(',')[2].Split('=')[1].Trim();
            li = new System.Web.UI.WebControls.ListItem(nomeBrand+" - "+nomeModello, idModello);
            li.Attributes["OptionGroup"] = nomeBrand;
            ddl.Items.Add(li);
            cont++;

        }
    }
the problem is that, when I watch the source code I do not have the optgroup tag but only options tag.
In fact, if I put a breakpoint in the first line of the method RenderContents this doesn't fire. What I'm doing wrong?
Source : https://forums.asp.net/t/2127882.aspx?Dropdownlist+with+optgroup+using+WebControlAdapters

https://www.codeproject.com/Articles/15505/ASP-NET-DropDownList-with-OptionGroup-support

No comments:

Post a Comment