您的位置:首页 > 编程语言 > ASP

重载asp.net的dropdownlist控件,使其支持样式和空格的一点改进

2011-02-12 14:57 447 查看
参考:http://kb.cnblogs.com/a/391555/

这个代码很好用,让呆板的DropDownList可以添加漂亮的样式,并且支持空格.

也很有代表性,可以让大家举一反三,灵活使用其他asp控件.

但在应用的时候,发现了一点小问题:

原作代码用(char)58853来替换" ",效果看上去是像空格,但并不是空格,如果浏览器切换编码,就会显示乱码.

为了解决这个问题,我对原代码稍做修改,就可以任意切换都不会有问题.修改后的代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.ComponentModel;
using System.Collections;
using System.Drawing;

 

namespace reworkControl

{
    /// <summary>
    /// DropDownLists的摘要说明
    /// </summary>
    public class MyDropDownLists : 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('>');

                    //start            一直到"//end",是修改的地方
                    //HttpUtility.HtmlEncode(listItem.Text.Replace(" ", ((char)58853).ToString()), writer);
                    string tmp = listItem.Text.Replace(" ", "§");//这里的"§"尽量用偏僻的字符 

                    char[] pt = tmp.ToCharArray();
                    for (int i = 0; i < pt.length; i++)
                    {
                        if (pt[i] == '§')
                        {
                            writer.Write(" ");
                        }
                        else
                        {
                            writer.Write(pt[i]);
                        }
                    }

                    //end

                    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());
                }
            }
        }

    }
}


下面对使用方法归纳整理下,便于初学者使用:

1> 将上面的代码单独保存为.cs文件,比如MyDropdownlist.cs

2>把文件MyDropdownlist.cs拷贝到C:/WINDOWS/Microsoft.NET/Framework/v3.5文件下

(相信大家的Framework都是3.5了, 如果不是,请选相应的文件夹).

3>运行->程序->附件->dos命令控制台,打开命令窗后,输入cd C:/WINDOWS/Microsoft.NET/Framework/v3.5,

回车进入该目录,再输入csc /t:library MyDropdownlist.cs,回车.

4>把该目录下生成的MyDropdownlist.dll拷贝到你的工程文件里面的/WEB/BIN文件夹下.

5>需要使用该MyDropdownlist控件的页面文件(.aspx)顶部添加如下代码:



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST.aspx.cs" Inherits="WebApplication.TEST" %>
<%@ Register TagPrefix="myDDL" Namespace="reworkControl" Assembly="MyDropdownlist" %> //在这个位置添加这行代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
... ...

6>.aspx页面调用的代码如下:

<myDDL:MyDropdownlist ID="xxx" runat="server" Width="12px" DataTextField="Name" DataValueField="Id" />

//跟调用asp原控件是一样的,<asp:dropdownlis id="xxx" runat="server" ... ... />

7>页面后台控制代码例子(照搬原例子):

protected void Page_Load(object sender, EventArgs e)
{
ListItem item = new ListItem("  萧萧", "bb");
item.Attributes.Add("style", "color:#ff3939");
item.Attributes.Add("onClick", "alert('你好')");
drp.Items.Add(item);
item = new ListItem("  欢欢", "cj");
item.Attributes.Add("style", "color:#ff39ee");
item.Attributes.Add("onclick", "alert('我好')");
drp.Items.Add(item);
}



相信大家都会用了吧,怎么样,知道怎么举一反三,改造其他控件来吗?

当然,不是特别需要,最好不要随便拓展控件哦.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: