In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of single-select and multi-select controls in Asp.net custom controls. I hope you will get something after reading this article. Let's discuss it together.
The details are as follows
It is also a good choice to package commonly used jquery plug-ins into controls.
Let's see the effect first:
1. Create a new class library project and create a data source class
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;}}
two。 Create a control class CheckList, inherit from WebControl, and define public List Items data item properties.
3. Introduce script files and style files
a. Create a script or style file, set the properties of the file-generate operations-embedded resources
b. You need to add the tag [assembly: WebResource ("Namespace. Folder name. file name", "mime type")] to the namespace
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 picture is also set to the embedded resource, which is written as follows in css
PerformSubstitution indicates whether the URL of other Web resources is analyzed during the processing of embedded resources and replaced with the full path of that resource.
c. Rewrite protected override void OnPreRender (EventArgs e) to introduce embedded scripts or style files
If (paginated null) Page.Header.Controls.Add (LiteralControl), put the tag in LiteralControl, then add LiteralControl to Page.Header, and finally you will see the introduced tag on 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 protected override void Render (HtmlTextWriter writer) method of a control
This is mainly the html of the rendering control, depending on your control.
Protected override void Render (HtmlTextWriter writer) {if (Items.Count > 0) {writer.Write (""); if (Multiple = = false) writer.Write (""); else writer.Write (""); bool first = true; foreach (var item in Items) {if (Multiple = = false) {if (item.Selected & first) {writer.Write ("" + item.Text + "); first = false } else {writer.Write ("" + item.Text + ");}} else {if (item.Selected) writer.Write ("+ item.Text +"); else writer.Write ("" + item.Text + "");}} writer.Write ("");}}
5. Add GetSelected method, return List, add GetSelectValue, return String (separated by multiple selections)
Public List 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 statu
You need to override two methods, protected override object SaveViewState () and protected override void LoadViewState (object savedState), 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) vState [1];}
7. Radio and check settings, controlled in js
Add attribute
[Description ("get and set multiple selections"), DefaultValue (true), Browsable (true), Category ("miscellaneous")]
Public bool Multiple {get; set;}
In the OnPreRender code, you will find that the Multiple attribute will affect the value of the mul attribute of div, thus determining whether to select multiple selections (default multiple selections).
8. Other instructions
Private static readonly string STYLE_TEMPLATE = "\ r\ n"
Private static readonly string SCRIPT_TEMPLATE = "\ r\ n"
Effect picture:
After reading this article, I believe you have a certain understanding of "sample analysis of single-select and multi-select controls in Asp.net custom controls". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.