Reload the dropdownlist control of Asp.net to make it support style and space improvement.

Source: Internet
Author: User

Reference: http://kb.cnblogs.com/a/391555/

This code is very useful, so that the rigid dropdownlist can add beautiful styles and support spaces.

It is also representative and allows you to use other ASP controls flexibly.

However, a small problem was found during the application:

The original code uses (char) 58853 to replace "& nbsp;". The effect looks like space, but not space. If the browser switches the encoding, garbled characters are displayed.

To solve this problem, I made a slight modification to the original code so that no problem could occur during any switchover. The modified code is as follows:

Using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; </P> <p> using system. componentmodel; <br/> using system. collections; <br/> using system. drawing; </P> <p> namespace reworkcontrol </P> <p> {<br/> /// <summary> <br/> // Summary of dropdownlists <br />/// </Summary> <br/> public class mydropdownlists: dropdownlist <br/>{< br/> protected override void rendercontents (htmltextwriter writer) <br/>{< br/> listitemcollection = base. items; <br/> int I = base. items. count; <br/> bool flag = false; <br/> if (I> 0) <br/> {<br/> for (Int J = 0; j <I; j ++) <br/>{< br/> listitem = listitemcollection [J]; <br/> writer. writebegintag ("option"); <br/> If (listitem. selected) <br/>{< br/> If (FLAG) <br/>{< br/> throw new httpexception ("cant_multiselect_in_dropdownlist "); <br/>}< br/> flag = true; <br/> writer. writeattribute ("selected", "selected", false); </P> <p >}< br/> writer. writeattribute ("value", listitem. value, true); <br/> system. web. UI. attributecollection = listitem. attributes; <br/> ienumerator = attributecollection. keys. getenumerator (); <br/> while (ienumerator. movenext () <br/>{< br/> string str2 = (string) ienumerator. current; <br/> writer. write ("" + str2 + "=/" "+ attributecollection [str2] +"/""); <br/>}< br/> writer. write ('>'); </P> <p> // start until "// end", where it is modified <br/> // httputility. htmlencode (listitem. text. replace ("", (char) 58853 ). tostring (), writer); <br/> string TMP = listitem. text. replace ("", "§"); // here, "§" tries to use remote characters </P> <p> char [] PT = TMP. tochararray (); <br/> for (INT I = 0; I <PT. length; I ++) <br/>{< br/> If (PT [I] = '§ ') <br/>{< br/> writer. write (""); <br/>}< br/> else <br/>{< br/> writer. write (PT [I]); <br/>}</P> <p> // end </P> <p> writer. writeendtag ("option"); <br/> writer. writeline (); <br/>}</P> <p> protected override object saveviewstate () <br/> {<br/> object [] objs = new object [2]; <br/> objs [0] = base. saveviewstate (); <br/> system. collections. arraylist list = new arraylist (); <br/> objs [1] = List; <br/> foreach (listitem item in this. items) <br/>{< br/> system. collections. hashtable hash = new hashtable (); <br/> foreach (Object key in item. attributes. keys) <br/>{< br/> hash. add (Key, item. attributes [key. tostring ()]); <br/>}< br/> list. add (hash); <br/>}< br/> return objs; <br/>}< br/> protected override void loadviewstate (Object savedstate) <br/>{< br/> object [] objs = (object []) savedstate; <br/> base. loadviewstate (objs [0]); <br/> system. collections. arraylist list = (system. collections. arraylist) objs [1]; <br/> for (INT I = 0; I <list. count; I ++) <br/>{< br/> system. collections. hashtable hash = (system. collections. hashtable) list [I]; <br/> foreach (Object key in hash. keys) <br/>{< br/> items [I]. attributes. add (key. tostring (), hash [Key]. tostring (); <br/>}</P> <p >}< br/>}</P> <p>

The following is a summary of the usage methods for beginners:

1> Save the preceding code as a. CS file, for example, mydropdownlist. CS.

2> copy the mydropdownlist. CS file to the C:/Windows/Microsoft. NET/framework/v3.5 file.

(I believe all the frameworks are 3.5. If not, select the corresponding folder ).

3> Run-> Program-> attachment-> dos console. Open the command window and enter cd c:/Windows/Microsoft. NET/framework/v3.5,

Enter the directory, enter CSC/T: Library mydropdownlist. CS, and press Enter.

4> copy the mydropdownlist. dll generated in this directory to the/web/bin folder in your project file.

5> Add the following code at the top of the page file (. aspx) of the mydropdownlist control:

 

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "test. aspx. cs" inherits = "webapplication. Test" %>
<% @ Register tagprefix = "myddl" namespace = "reworkcontrol" assembly = "mydropdownlist" %> // Add this line of code to this location
<! 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>. The code for calling the ASPX page is as follows:

<Myddl: mydropdownlist id = "XXX" runat = "server" width = "12px" datatextfield = "name" datavaluefield = "ID"/>

// It is the same as calling the original ASP control. <asp: dropdownlis id = "XXX" runat = "server" ....../>

7> page background control code example (copy the original example ):

Protected void page_load (Object sender, eventargs E)
{
Listitem item = new listitem ("& nbsp; Xiao", "BB ");
Item. Attributes. Add ("style", "color: # ff3939 ");
Item. Attributes. Add ("onclick", "alert (' ')");
DRP. Items. Add (item );
Item = new listitem ("& nbsp; Huanhuan", "CJ ");
Item. Attributes. Add ("style", "color: # ff39ee ");
Item. Attributes. Add ("onclick", "alert ('Hello me ')");
DRP. Items. Add (item );
}

 

I believe everyone will use it. How do I know how to transform other controls?

Of course, it is not especially necessary. It is best not to expand the control at will.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.