2017-08-30 47 views
0

我需要使用optgroup做下拉列表。 我發現很多導遊和所有預見的使用WebControlAdapter this is the guide that I'm fllowing使用WebControlAdapters的optgroup下拉列表

我已經添加了類,我App_Code文件夾中的項目:

namespace admin.App_Code 
{ 
    public class DropDownListAdapter : 
    System.Web.UI.WebControls.Adapters.WebControlAdapter 
    { 
    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) 
    { 
     // The current control being "adaptered" is available within context from the Control property 
     DropDownList dropDownList = (DropDownList)Control; 
     ListItemCollection items = dropDownList.Items; 

     // Retrieve Optgrouping using LinQ 
     var groups = (from p in items.OfType<ListItem>() 
         group p by p.Attributes["Group"] into g 
         select new { Label = g.Key, Items = g.ToList<ListItem> 
     () }); 

     foreach (var group in groups) 
     { 
      if (!String.IsNullOrEmpty(group.Label)) 
      { 
       writer.WriteBeginTag("optgroup"); 
       writer.WriteAttribute("label", group.Label); 
       writer.Write(">"); 
      } 

      int count = group.Items.Count(); 
      if (count > 0) 
      { 
       bool flag = false; 
       for (int i = 0; i < count; i++) 
       { 
        ListItem item = group.Items[i]; 

        writer.WriteBeginTag("option"); 
        if (item.Selected) 
        { 
         if (flag) 
         { 
          throw new HttpException("Multiple selected items not allowed"); 
         } 
         flag = true; 

         writer.WriteAttribute("selected", "selected"); 
        } 

        if (!item.Enabled) 
        { 
         writer.WriteAttribute("disabled", "true"); 
        } 

        writer.WriteAttribute("value", item.Value, true); 

        if (this.Page != null) 
        { 
         this.Page.ClientScript.RegisterForEventValidation(dropDownList.UniqueID, item.Value); 
        } 
        writer.Write('>'); 
        HttpUtility.HtmlEncode(item.Text, writer); 
        writer.WriteEndTag("option"); 
        writer.WriteLine(); 
       } 
      } 
      if (!String.IsNullOrEmpty(group.Label)) 
      { 
       writer.WriteEndTag("optgroup"); 
      } 
     } 
    } 

    private Object _ViewState; 

    protected override void OnLoad(EventArgs e) 
    { 
     if (Page.IsPostBack) 
     { 
      if (_ViewState != null) 
      { 
       Object[] groups = (Object[])_ViewState; 
       DropDownList dropDownList = (DropDownList)Control; 
       // Add saved optgroups to ListItems 
       for (Int32 i = 0; i < groups.Length; i++) 
       { 
        if (groups[i] != null) 
        { 
         dropDownList.Items[i].Attributes["Group"] = groups[i].ToString(); 
        } 
       } 
      } 
     } 
     base.OnLoad(e); 
    } 

    protected override void LoadAdapterViewState(object state) 
    { 
     // Retrieve existing state 
     _ViewState = state; 
    } 

    protected override object SaveAdapterViewState() 
    { 
     DropDownList dropDownList = (DropDownList)Control; 
     Int32 count = dropDownList.Items.Count; 
     Object[] values = new Object[count]; 

     // Retrieve Optgrouping from ListItem 
     for (int i = 0; i < count; i++) 
     { 
      values[i] = dropDownList.Items[i].Attributes["Group"]; 
     } 
     return values; 
    } 
    } 
} 

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 = ""; 

     List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(); 
     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["Group"] = nomeBrand; 
      items.Add(li); 
      cont++; 
     }; 


     ddl.DataSource = items; 
     ddl.DataBind(); 

     ddl.SelectedIndex = -1; 
    } 

我已經添加的文件夾App_Browser到我的項目(沒不存在的),我已經添加了文件BrowserFile.browser

<browsers> 
    <browser refID="Default"> 
    <controlAdapters> 
     <adapter controlType="System.Web.UI.WebControls.DropDownList" 
     adapterType="admin.App_Code.DropDownListAdapter" /> 
    </controlAdapters> 
    </browser> 
</browsers> 

在我的網頁的.aspx(即在類DropDownListAdapter的同一文件夾中我有

<asp:DropDownList runat="server" ID="ddlModelli" CssClass="form-control multipleSelect"></asp:DropDownList> 

填充在這樣

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 = ""; 

     List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(); 
     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["Group"] = nomeBrand; 
      items.Add(li); 
      cont++; 
     }; 


     ddl.DataSource = items; 
     ddl.DataBind(); 

     ddl.SelectedIndex = -1; 
    } 

的問題是,當我看源代碼我沒有OPTGROUP標籤,但唯一的選項標籤。

實際上,如果我在方法RenderContents的第一行中放置斷點,則不會觸發。 我做錯了什麼?

回答

0

我解決了這個問題。 問題出在LoadDDLModelli方法中。 而不是設置DataSource和DataBind到通過引用傳遞的Dropdownlist,我必須添加ItemList singoularly(我無法理解差異)

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 = ""; 

    List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(); 
    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["Group"] = nomeBrand; 
     items.Add(li); 
     cont++; 
    }; 


    //ddl.DataSource = items; 
    //ddl.DataBind(); 

    foreach(ListItem i in items) 
     ddl.Items.Add(i); 

    ddl.SelectedIndex = -1; 
}