现在的位置: 首页 > 综合 > 正文

[原创]实现给DropDownList下拉框控件子项增加自己Attribute特性…

2013年05月26日 ⁄ 综合 ⁄ 共 7129字 ⁄ 字号 评论关闭

   前不久,看到腾讯站上有个下拉框控件里面子项的字体都是五颜六色,感觉挺好看的!然后想想也想做一个,也学习一下,组件设计不是很了解!看过一些书..望大家多多指教!

   但是中间遇到不少问题,非常感谢Haneryfan1朋友的帮助!现把代码帖出...

Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Drawing
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls

Namespace hackate
    
Public Class DropDownListColor
        
Inherits DropDownList

        
Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            
Dim collection1 As ListItemCollection = Me.Items
            
Dim num1 As Integer = Me.Items.Count
            
Dim flag1 As Boolean = False
            
If (num1 > 0Then
                
Dim num2 As Integer
                
For num2 = 0 To num1 - 1
                    
Dim item1 As ListItem = collection1.Item(num2)
                    writer.WriteBeginTag(
"option")
                    
If item1.Selected Then
                        
If flag1 Then
                            
Throw New HttpException("Cant_Multiselect_In_DropDownList")
                        
End If
                        flag1 
= True
                        writer.WriteAttribute(
"selected""selected"False)
                    
End If
                    writer.WriteAttribute(
"value", item1.Value, True)
                    
'Dim abCollection As System.ComponentModel.AttributeCollection = TypeDescriptor.GetAttributes(item1)
                    Dim ie As IEnumerator = item1.Attributes.Keys.GetEnumerator

                    
Dim str2 As Object
                    
Do While ie.MoveNext
                        str2 
= ie.Current
                        writer.WriteAttribute(str2.ToString, item1.Attributes.Item(str2.ToString))
                    
Loop
                    
'Dim itstyle As String = item1.Attributes.Item("style")
                    'writer.WriteAttribute("style", itstyle)
                    writer.Write(">"c)

                    writer.
Write(item1.Text)
                    writer.WriteEndTag(
"option")
                    writer.
WriteLine()
                
Next num2
            
End If
        
End Sub


        
'Public ReadOnly Property ItemStyles() As ArrayList
        '    Get
        '        If viewstate.Item("styles") Is Nothing Then viewstate.Item("styles") = New ArrayList
        '            Dim AR As ArrayList = viewstate.Item("styles")
        '            If AR.Count <> Items.Count Then
        '            For N As Integer = AR.Count + 1 To Items.Count
        '                AR.Add(New Style)
        '            Next
        '            viewstate.Item("styles") = AR
        '        Else
        '            viewstate.Item("styles") = AR
        '        End If
        '        Return AR
        '    End Get
        'End Property

        
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
            
Dim o As Object() = CType(savedState, Object())
            
MyBase.LoadViewState(o(0))
            
Dim list As ArrayList = CType(o(1), ArrayList)
            
For i As Integer = 0 To list.Count - 1
                
Dim hash As Hashtable = CType(list(i), Hashtable)
                
For Each key As Object In hash.Keys
                    Items(i).Attributes.Add(key.ToString, hash(key.ToString))
                
Next
            
Next
        
End Sub


        
Protected Overrides Function SaveViewState() As Object
            
Dim o(2As Object
            o(
0= MyBase.SaveViewState
            
Dim list As ArrayList = New ArrayList
            o(
1= list
            
For Each item As ListItem In Me.Items
                
Dim hash As Hashtable = New Hashtable
                
For Each key As Object In item.Attributes.Keys
                    hash.Add(key, item.Attributes(key.ToString))
                
Next
                list.Add(hash)
            
Next
            
Return o
        
End Function

    
End Class

End Namespace

我知道大多都用C#开发的,我也附上一个C#的.

public class MyDrop :DropDownList
    
{
        
protected override void RenderContents(HtmlTextWriter writer)
        
{
            ListItemCollection listItemCollection 
= base.Items;
            
int i = base.Items.Count;
            
bool flag = false;
            
if (i > 0)
            
{
                
for (int j = 0; j < i; j++)
                
{
                    ListItem listItem 
= listItemCollection[j];
                    writer.WriteBeginTag(
"option");
                    
if (listItem.Selected)
                    
{
                        
if (flag)
                        
{
                            
throw new HttpException("Cant_Multiselect_In_DropDownList");
                        }

                        flag 
= true;
                        writer.WriteAttribute(
"selected""selected"false);


                    }

                    writer.WriteAttribute(
"value", listItem.Value, true);
                    System.Web.UI.AttributeCollection attributeCollection 
= listItem.Attributes;
                    IEnumerator iEnumerator 
= attributeCollection.Keys.GetEnumerator();
                    
while (iEnumerator.MoveNext())
                    
{
                        
string str2 = (String)iEnumerator.Current;
                        writer.Write(
" "+str2+"=\""+attributeCollection[str2]+"\"");
                    }

                    writer.Write(
'>');
                    HttpUtility.HtmlEncode(listItem.Text, writer);
                    writer.WriteEndTag(
"option");
                    writer.WriteLine();
                }

            }

        }


        
protected override object SaveViewState()
        
{
            
object[] objs = new  object[2];
            objs[
0]= base.SaveViewState ();
            System.Collections.ArrayList list 
= new ArrayList();
            objs[
1]= list;
            
foreach(ListItem item in this.Items)
            
{
                System.Collections.Hashtable hash 
= new Hashtable();
                
foreach(Object key in item.Attributes.Keys)
                
{
                    hash.Add(key,item.Attributes[key.ToString()]);
                }

                list.Add(hash);
            }

            
return objs;
        }

        
protected override void LoadViewState(object savedState)
        
{
            
object[] objs = (Object[])savedState;
            
base.LoadViewState (objs[0]);
            System.Collections.ArrayList list 
= (System.Collections.ArrayList)objs[1];
            
for(int i =0;i< list.Count;i++)
            
{
                System.Collections.Hashtable hash 
= (System.Collections.Hashtable)list[i];
                
foreach(object key in hash.Keys)
                
{
                    Items[i].Attributes.Add(key.ToString(),hash[key].ToString());
                }

            }

            
        }




    }

然后使用这个控件的时候只要
Dim item As New ListItem("苯苯", "bb")
            item.Attributes.Add("style", "color:#ffee39")
            item.Attributes.Add("onClick", "alert('你好')")
            ddl.Items.Add(item)
            item = New ListItem("超级", "cj")
            item.Attributes.Add("style", "color:#ff39ee")
            item.Attributes.Add("onclick", "alert('我好')")
            ddl.Items.Add(item)
这样则可!
=========================================
在CNBLOGS里有很多高手,如果代码有不足等请大家提出意见,如果有更好的解决方案,请多多提出,谢谢了.......

抱歉!评论已关闭.