SortedList sl = new SortedList();
//type1
sl.Add("AAA","text1");
sl.Add("CCC","text3");
sl.Add("DDD","text4");//type2
sl.Add("BBB","text2");
sl.Add("GGG","text7");
sl.Add("HHH","text8");//type3
sl.Add("EEE","text5");
sl.Add("FFF","text6");
//开始绑定Dropdown List
dropdownList1.DataSource = sl;
dropdownList1.DataTextField = "key";
dropdownList1.DataValueField = "value";
dropdownList1.DataBind();//DropdownList显示结果为
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
//问题
因为分了三个Type,我同时还想把Type也加进去
实现这样的排序方式:
**其中Type1 Type2 Type3这三个值让用户没有办法选择,就是点上去没有反应,不知道能不能实现
//DropdownList显示结果为
Type1
AAA
CCC
DDD
Type2
BBB
GGG
HHH
Type3
EEE
FFF
//有挑战吧?

解决方案 »

  1.   

    有两个难点需要实现:
    1,如何克服SortedList本身的排序规则,按照我们自己的方式去排序
    2,如何把Type1 Type2 Type3这三个选项让用户看得到,但是无法选择
      

  2.   

    DropdownList 的SelectedIndexChanged事件中加入
    if(this.DropdownList.text == "Type1" || this.DropdownList.text == "Type2" || this.DropdownList.text == "Type3" )
        this.this.DropdownList.text = "";
      

  3.   

    <optgroup></optgroup>http://www.dreamdu.com/xhtml/tag_optgroup/
      

  4.   

    示例<form action="dreamdu.php" method="post" id="dreamduform">
    <label for="WebDesign">选择一个你在梦之都最想学的</label>
    <select id="WebDesign" name="WebDesign">
    <optgroup label="client">
    <option value="HTML">HTML</option>
    <option value="CSS">CSS</option>
    <option value="javascript">javascript</option>
    </optgroup>
    <optgroup label="server">
    <option value="PHP">PHP</option>
    <option value="ASP">ASP</option>
    <option value="JSP">JSP</option>
    </optgroup>
    <optgroup label="database">
    <option value="Access">Access</option>
    <option value="MySQL">MySQL</option>
    <option value="SQLServer">SQLServer</option>
    </optgroup>
    </select>
    </form>
      

  5.   

    第一个可以用 Dictionary<string, string>来代替SortedList,如果没有特殊的要求
    第二个如果是选择上去没有反应,那可以在DropDownList的SelectedIndexChanged事件中加一个判断,比如:
     if (!this.DropDownList1.SelectedValue.ToString().StartsWith("Type"))
            {
                //执行的操作
            }
    ----------------------------------------------------------------------
    期待更好的方法
      

  6.   

    没怎么看懂!!
    type1,type2..是不是指text1,text2的类型不同??
      

  7.   


    不是,type1 type2 只是为了让用户操作方便,dropdownlist里的数据可能有几十条,用户找起来很麻烦,所以就加了个文字说明(type1,type2),可以把type1想象成山东省,下面的AAA什么的都是城市名称
      

  8.   


    第一个我已经采取你提供的方法了,第二个问题,可不可以实现把type加粗或变灰,让用户点上去没有反应?
      

  9.   

    第二个问题你可以这样:用户点上去之后在selectedindexchanged事件(类似,具体不太记得)里判断选的是不是TYPe,是就清空,再把下拉列表显示出来(用代码,WEB不知道行不行,WIN下的COMBOBOX是可以的).
      

  10.   

    改变颜色其实很简单
       public static void SetDropDownListStyle(DropDownList  objlist)
        {
            foreach (ListItem listitem in objlist.Items)
            {
                if (listitem.Value.ToString().StartsWith("Type"))
                {
                    listitem.Attributes.Add("style", "color:#E4E2D5");
                }
            }
        }
    绑定后调用就行了
    DropDownList1.DataSource = sl;
    DropDownList1.DataTextField = "key";
    DropDownList1.DataValueField = "value";
    DropDownList1.DataBind();
    SetDropDownListStyle(this.DropDownList1);
    另外在dropdownlist事件中也要调用一下:
     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           //选择是Type的时候,不执行操作
            if (!this.DropDownList1.SelectedValue.ToString().StartsWith("Type"))
            {
                //你的操作
            }
            SetDropDownListStyle(this.DropDownList1);
        }
    那个没有选择没有反应,也可以从javascript上考虑屏蔽鼠标事件
      

  11.   

    ListItem li=new ListItem("","");
        DropDownList1.Items.Insert(i,li);