Add Collection collection properties to the ASP.net component (C #)

Source: Internet
Author: User
Tags bool constructor
Asp.net| collection You may have used the DropDownList control in ASP.net, which has a collection property for the items that you use to set the Drop-down options for the combo box. This thoughtful approach makes the user feel more comfortable, and now we are also playing with our talents to create a property of the same function.

The control we're about to create is called WebPanel, which is used for Web site navigation and can shrink to look like this:



The first figure is the effect of the contraction, the second is the effect of contraction, although not flashy, but enough to explain the problem.

Next look at a picture:




This is the editor of the collection attribute, and today we're going to start talking about this effect.



In the editor of the collection property above, the main part is divided into two parts, the left part is the object's collection, the right part is the selected object's property, we can set the assignment to each property.



First, we define a class: Stringitem, which has two custom attributes: text for display, hypertext is a navigation hyperlink.

Using System;

Using System.Web.UI;



Namespace Nswebpanel

{

<summary>

Summary description of the Stringitem.

</summary>

///

public class StringItem:System.Web.UI.Control, IStateManager

{

private string _text;

private string _hypertext;



Public Stringitem ()

{

//

TODO: Add constructor logic here

//

}



public string Text

{

Set

{

_text = value;

}

Get

{

return _text;

}

}



public string Hypertext

{

Set

{

_hypertext = value;

}

Get

{

return _hypertext;

}

}

#region IStateManager Members



void Istatemanager.trackviewstate ()

{

Base. TrackViewState ();

}



BOOL Istatemanager.istrackingviewstate

{

Get

{

Return base. isTrackingViewState;

}

}



Object Istatemanager.saveviewstate ()

{

Return base. SaveViewState ();

}



void Istatemanager.loadviewstate (object state)

{

Base. LoadViewState (state);

}



#endregion

}

}



Then, you create a class that holds multiple Stringitem objects: Stringitems, you need to be aware that the class inherits collectionbase so that you can use the default collection property editor.

Using System;

Using System.Collections;

Using System.Web.UI;



Namespace Nswebpanel

{

<summary>

Summary description of the Stringitems.

</summary>

public class Stringitems:collectionbase, IStateManager

{

private bool marked;



Public Stringitems (): Base ()

{

//

TODO: Add constructor logic here

//

}



private void Initialize ()

{

marked = FALSE;

}



Public Stringitem This[int Index]

{

Get

{

Return (Stringitem) base. List[index];

}

Set

{

List[index] = value;

}

}



public void Add (Stringitem aitem)

{

Base. List.add (AItem);

}



public void Remove (int index)

{

if (Index < base. Count-1 && Index > 0)

{

Base. List.removeat (index);

}

}

#region IStateManager Members



void Istatemanager.trackviewstate ()

{

for (int i = 0; i < base. List.count; i + +)

{

((IStateManager) base. List[i]). TrackViewState ();

}

}



BOOL Istatemanager.istrackingviewstate

{

Get

{

return marked;

}

}



Object Istatemanager.saveviewstate ()

{

object[] istate = new object[base. List.count];

for (int i = 0; i < base. List.count; i + +)

{

Istate[i] = ((IStateManager) base. List[i]). SaveViewState ();

}

return istate;

}



void Istatemanager.loadviewstate (object state)

{

if (state!= null)

{

Object[] ViewState = (object[]) state;

for (int i = 0; i < viewstate.length i + +)

{

((IStateManager) list[i]). LoadViewState (Viewstate[i]);

}

}

}

#endregion

}

}

Finally, create a new Web control library with the name: Nswebpanel and the following is the source code:

Using System;

Using System.Web.UI;

Using System.Collections;

Using System.Web.UI.WebControls;

Using System.ComponentModel;



Namespace Nswebpanel

{

<summary>

Summary description of the WebCustomControl1.

</summary>



public class Webcustomcontrol1:system.web.ui.webcontrols.webcontrol,inamingcontainer,istatemanager

{

Private Const string SCRIPT = "<table id= ' t ' border= ' 1 ' width= ' 228 ' height= ' 145 ' bordercolor= '" #000000 ' cellspacing= ' 0 ' cellpadding= ' 0 ' bordercolorlight= ' #000000 ' bordercolordark= ' #FFFFFF ' >\n ' +

"<tr>\n" +

"<td width= ' 228 ' height= ' >\n" +

"<table border= ' 0 ' width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' >\n" +

"<tr>\n" +

"<td width= ' 10% ' bgcolor= ' #CCCCFF ' id= ' sign '; \ n" +

"}\n" +



"flag =!flag;\n" +

"}\n" +

"</script>\n";





Private Stringitems _strings;

Notice the red section below, make sure you write it.

[

PersistenceMode (Persistencemode.innerproperty),

DesignerSerializationVisibility (designerserializationvisibility.content)

]

Public Stringitems Strings

{

Get

{

if (_strings = null)

_strings = new Stringitems ();

return _strings;

}



}



private string _caption;

public string Caption

{

Get

{

return _caption;

}

Set

{

_caption = value;

}

}



#region Events



private static Object _fieldevent = NULL;

Public event System.EventHandler processevent

{

Add

{

Events.addhandler (_fieldevent,value);

}

Remove

{

Events.removehandler (_fieldevent,value);

}

}



#endregion



public void Execute ()

{

EventHandler hander = (EventHandler) events[_fieldevent];

If (hander!= null)

{

Hander (This,null);

}

}



public override void RenderBeginTag (HtmlTextWriter writer)

{

Base. RenderBeginTag (writer);

System.Text.StringBuilder str = new System.Text.StringBuilder ();

Str. AppendFormat (script,this. Caption,_strings[0]. TEXT,_STRINGS[1]. TEXT,_STRINGS[2]. TEXT,_STRINGS[3]. TEXT,_STRINGS[4]. Text,_strings[0]. HYPERTEXT,_STRINGS[1]. HYPERTEXT,_STRINGS[2]. HYPERTEXT,_STRINGS[3]. HYPERTEXT,_STRINGS[4]. hypertext);

Writer. WriteLine (str. ToString ());

}



protected override void OnPreRender (EventArgs e)

{

Base. OnPreRender (e);

if (! Page.isclientscriptblockregistered ("13c165f4-db2e-4484-ab1b-5b1f32c8fc8b"))

{

Page.registerclientscriptblock ("13c165f4-db2e-4484-ab1b-5b1f32c8fc8b", JAVASCRIPT);

}

}





public override string ClientID

{

Get

{

Return base. ClientID + "LZH";

}

}



protected override void AddAttributesToRender (HtmlTextWriter writer)

{

Base. AddAttributesToRender (writer);

Writer. AddStyleAttribute ("Font-size", "14px");

}







protected override HtmlTextWriterTag Tagkey

{

Get

{

return htmltextwritertag.div;

}

}

#region IStateManager Members



void Istatemanager.trackviewstate ()

{

Base. TrackViewState ();



}



BOOL Istatemanager.istrackingviewstate

{

Get

{

TODO: Add Webcustomcontrol1.istrackingviewstate Getter Implementation

return false;

}

}



Object Istatemanager.saveviewstate ()

{

TODO: Add webcustomcontrol1.saveviewstate implementation

return null;

}



void Istatemanager.loadviewstate (object state)

{

TODO: Add webcustomcontrol1.loadviewstate implementation

}



#endregion

}

}





Related Article

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.