ASP. NET Server Control checkboxlist Modification

Source: Internet
Author: User
Background: The Server Control checkboxlist that comes with Asp.net does not display the name value when generating HTML. In this way, after the form is submitted, request. form (which is obtained based on the name value) cannot obtain the selected value. Therefore, this control is slightly modified.
Using system; using system. collections. generic; using system. componentmodel; using system. LINQ; using system. text; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. collections. specialized; using system. text. regularexpressions; namespace checkbox {// <summary> /// Principle: The checkboxlist of VS has no value. Therefore, this control is available. // by: xcl @ 2012.9 QQ: 80213876 http://blog.csdn.net/luoyeyu1989 (retain if you need to modify this control This line of information is enough, thank you) // </Summary> [defaultproperty ("text")] [toolboxdata ("<{0 }: servercontrol1 runat = Server> </{0}: servercontrol1> ")] public class checkbox: checkboxlist {Private Static string getguid =" _ "+ guid. newguid (). tostring ("N"); protected override void onload (eventargs e) {# region sets the default selected item if (! String. isnullorempty (this. setselectedvalues) {string [] Vs = This. setselectedvalues. split (','); If (. length> 0) {for (INT I = 0; I <. length; I ++) {If (vs [I]. length> 0) {If (this. items. count> 0) {for (Int J = 0; j <this. items. count; j ++) {If (string. equals (this. items [J]. value, vs [I], stringcomparison. ordinalignorecase) {This. items [J]. selected = true; break ;}}}}}# endregion Base. onload (e) ;}/// <summary> // use this as the name of each item /// </Summary> Public String getname {get {return getguid + this. uniqueid. replace ('$ ','_');}} # region style // <summary> // ul class // </Summary> Public String ulclass {Get; set ;} /// <summary> // class of Li /// </Summary> Public String liclass {Get; set ;} /// <summary> // UL's style /// </Summary> Public String ulstyle {Get; Set ;}/// <Summary> // style of Li /// </Summary> Public String listyle {Get; set ;} # endregion # other region information /// <summary> /// (if you need to modify this control, keep this information. Thank you) /// </Summary> Public override void renderbegintag (htmltextwriter writer) {writer. write ("<! -- ****************** By xcl @ 2012.9 http://blog.csdn.net/luoyeyu1989************-- -- ** --> "); base. renderbegintag (writer) ;}/// <summary> /// (if you want to modify this control, keep this information. Thank you) /// </Summary> Public override void renderendtag (htmltextwriter writer) {writer. write ("<! -- ****************** By xcl @ 2012.9 http://blog.csdn.net/luoyeyu1989************-- -- ** --> "); base. renderendtag (writer) ;}# endregion private string _ selectedvalues = string. empty; // <summary> // sets the default selected values (separated by commas) /// </Summary> Public String setselectedvalues {Get; set ;} /// <summary> /// render HTML /// </Summary> protected override void render (htmltextwriter output) {// base. render (writer); // do not add this sentence. After adding this sentence, the original Stringbuilder STR = new stringbuilder (); If (this. Items. Count> 0) {string ulcss = (string. isnullorempty (this. ulstyle )? "Style = 'width: 100%; List-style-type: none; '": string. format ("style = '{0}'", this. ulstyle) + (string. isnullorempty (this. ulclass )? "": String. Format ("class = '{0}'", this. ulclass); string licss = (string. isnullorempty (this. listyle )? "Style = 'width: 33%; List-style-type: none; float: Left; '": string. format ("style = '{0}'", this. listyle) + (string. isnullorempty (this. liclass )? "": String. format ("class = '{0}'", this. liclass); Str. appendformat ("<ul {0}>", ulcss); For (INT I = 0; I <this. items. count; I ++) {Str. appendformat ("<li {4}> <input type = 'checkbox' id = '{0} {1} 'name =' {0} 'value = '{2 }' {5}/> <label for = '{0} {1}'> {3} </label> </LI> ", this. getname, I, this. items [I]. value, this. items [I]. text, licss, this. items [I]. selected? "Checked = 'checked'": "") ;}str. append ("</ul>") ;}output. Write (Str. tostring ());}}}

 

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.