asp.net custom control's single, multiple-selection controls _ Practical Tips

Source: Internet
Author: User

This example for you to share the ASP.net radio, check box control of the specific implementation code for your reference, the specific contents are as follows

It is also a good choice to encapsulate the common jquery plug-ins into controls.

First look at the effect:

1. New Class Library project, create data source class

 [Serializable]
 public class Select2item
 {public
 bool Selected {get; set;}

 public string Text {get; set;}

 public string Value {get; set;}

 Public Select2item () {} public

 Select2item (string text, string value)
 {this
  . Text = text;
  This. value = value;
 }

 Public Select2item (string text, string value, bool selected)
 {this
  . Text = text;
  This. value = value;
  This. Selected = Selected;
 }
  

2. Create control class checklist, inherit and WebControl, and define public list<select2item> items data item properties.

3. Introduction of script files and style files
A. Creating a script or style file, setting the properties of a file-build action-Embedded Resource

B. Need to add tags on namespace [Assembly:webresource (namespace. folder name. File name, MIME type)]
Such as:
[Assembly:webresource ("Control.Style.checklist.css", "text/css", Performsubstitution = True)]
[Assembly:webresource ("Control.Scripts.checklist.js", "Application/x-javascript")]

If there is a picture in the CSS file, the image is also set as an embedded resource, in the CSS <%=webresource ("namespace. Folder name. FileName")%>
Performsubstitution represents whether other Web resource URLs are parsed during processing of an embedded resource and replaced with the full path to that resource.
c. Rewrite protected override void OnPreRender (EventArgs e) to introduce an embedded script or style file
if (page!=null) Page.Header.Controls.Add (LiteralControl), place <script><link> label in LiteralControl, Then add LiteralControl to Page.header, and then you'll see the introduction of <script><link> tags in the page.

 protected override void OnPreRender (EventArgs e)
 {
  if (this. Page!= null)
  {
  StringBuilder SBB = new StringBuilder ();
  SBb. Append (String. Format (Style_template, Page.ClientScript.GetWebResourceUrl (this.) GetType (), "HandControl.Style.checklist.css"));
  SBb. Append (String. Format (Script_template, Page.ClientScript.GetWebResourceUrl (this.) GetType (), "HandControl.Scripts.checklist.js"));

  BOOL Hascss = false;
  LiteralControl LCC = new LiteralControl (SBB. ToString ());
  Lcc.id = "Lccheck";
  foreach (Control item in Page.Header.Controls)
  {
   if (item.id = = "Lccheck")
   hascss = true;
  }
  if (!hascss)
   Page.Header.Controls.Add (LCC);
  Base. OnPreRender (e);
 } 

4. Override the control's protected override void Render (HtmlTextWriter writer) method
This is primarily the HTML that renders the control, depending on your control.

 protected override void Render (HtmlTextWriter writer) {if (Items.Count > 0) {writer. Write ("<div id= ' div" + this.) ClientID + "' class= ' c01-tag-div ' mul= '" + (multiple = = true?)
  "1": "0") + "' >"); if (multiple = = false) writer. Write ("<input name= ' TB" + this.) ClientID + "' type= ' hidden ' value= '" + items[0].
  Value + "'/>"); else writer. Write ("<input name= ' TB" + this.)
  ClientID + "' type= ' hidden '/>");
  bool A = true; foreach (var item in Items) {if (multiple = False) {if (item). Selected && a) {writer. Write ("<a title= '" + Item.) Text + "' class= ' c01-tag-item c01-tag-select ' val= '" + Item. Value + "' tag= ' Y ' >" + item.
    Text + "</a>");
   i = false; else {writer. Write ("<a title= '" + Item.) Text + "' class= ' c01-tag-item ' val= '" + Item. Value + "' tag= ' N ' >" + item.
   Text + "</a>"); } else {if (item. Selected) writer. Write ("<a title= '" + Item.) Text + "' class= 'C01-tag-item c01-tag-select ' val= ' + item. Value + "' tag= ' Y ' >" + item.
   Text + "</a>"); else writer. Write ("<a title= '" + Item.) Text + "' class= ' c01-tag-item ' val= '" + Item. Value + "' tag= ' N ' >" + item.
   Text + "</a>"); }} writer.
  Write ("</div>"); 
 }
 }

5. Add GetSelected method, return list<select2item>, add Getselectvalue, return string (multiple-select, separated)

 Public list<select2item> getselected ()
 {
  if (Page!= null)
  {
  var values = page.request.form["TB" + this. ClientID]. Split (', ');
  var res = Items.where (t => values. Contains (T.value)). ToList ();
  foreach (var item in Items)
  {
   if (res). Contains (item))
   {
   item. Selected = true;
   }
   else
   {
   item. Selected = false;
   }
  }
  return res;
  }
  else
  {return
  null;
  }
 } 
 public string Getselectvalue ()
 {
  if (Page!= null)
  {return
  page.request.form[' TB ' + this. ClientID];
  }
  Return "";
 } 

6. Save State
you need to rewrite two methods protected override Object SaveViewState (), protected override void LoadViewState (object savedstate), Designed to save items data item properties to ViewState

 protected override Object SaveViewState ()
 {
  var valuestr = page.request.form["TB" + this. ClientID];
  if (!string. IsNullOrEmpty (VALUESTR))
  {
  var values = valuestr. Split (', ');
  var temp = items.where (t => values. Contains (T.value)). ToList ();
  foreach (var item in temp)
  {
   item. Selected = true;
  }
  }
  return new object[] {base. SaveViewState (), Items};

 protected override void LoadViewState (object savedstate)
 {
  object[] vstate = (object[)) savedstate;
  if (vstate[0]!= null)
  base. LoadViewState (Vstate[0]);
  if (vstate[1]!= null)
  Items = (list<select2item>) vstate[1];
 } 

7. Radio and check settings, in the JS control
Add Properties
[Description ("Get and set multiple selections"), DefaultValue (True), browsable (True), Category ("Miscellaneous")]
public bool Multiple {get; set;}
in OnPreRender code you will find that the multiple attribute affects the Mul property value of the div to determine whether to select multiple (default multiple selections)
8. Other Notes
private static readonly string style_template = "<link href=\" {0}\ "rel=\" stylesheet\ "type=\" text/css\ "/&" Gt;\r\n ";
private static readonly string script_template = "<script type=\ text/javascript\" src=\ "{0}\" ></script>\r \ n ";

Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.