为ASP.NET组件加上Collection集合属性(C#)
2024-07-10 13:00:17
供稿:网友
大家可能都用过asp.net中的dropdownlist控件,该控件有一个items的集合属性,用于设置组合框的下拉选项。这种体贴的做法让用户倍感温馨,现在,我们也发挥一下自己的的才智,创建一个相同功能的属性。
我们将要创建的控件叫webpanel,该控件用于网站导航,并且可以收缩,外观如下:
第一个图是未收缩的效果,第二个是收缩后的效果,虽然不炫,但是足可以说明问题了。
接下来再看一幅图:
这里,就是collection属性的编辑器,今天我们就围绕这种效果展开话题。
在上面的collection属性的编辑器中,主要分成两个部分,左边的部分是对象的集合,右边部分是选定对象的属性,我们可以对每个属性进行设置赋值。
首先,我们定义一个类:stringitem,该类有两个自定义属性:text用于显示的文本,hypertext是导航的超链接。
using system;
using system.web.ui;
namespace nswebpanel
{
/// <summary>
/// stringitem 的摘要说明。
/// </summary>
///
public class stringitem : system.web.ui.control , istatemanager
{
private string _text;
private string _hypertext;
public stringitem()
{
//
// todo: 在此处添加构造函数逻辑
//
}
public string text
{
set
{
_text = value;
}
get
{
return _text;
}
}
public string hypertext
{
set
{
_hypertext = value;
}
get
{
return _hypertext;
}
}
#region istatemanager 成员
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
}
}
然后,再创建一个用于存放多个stringitem对象的类:stringitems,需要注意的是:该类要继承collectionbase,这样才能使用默认的collection属性编辑器。
using system;
using system.collections;
using system.web.ui;
namespace nswebpanel
{
/// <summary>
/// stringitems 的摘要说明。
/// </summary>
public class stringitems : collectionbase , istatemanager
{
private bool marked;
public stringitems() : base()
{
//
// todo: 在此处添加构造函数逻辑
//
}
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 成员
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
}
}
最后,新建一个web控件库,名称为:nswebpanel,以下是源代码:
using system;
using system.web.ui;
using system.collections;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace nswebpanel
{
/// <summary>
/// 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='20'>/n" +
"<table border='0' width='100%' cellpadding='0' cellspacing='0'>/n" +
"<tr>/n" +
"<td width='10%' bgcolor='#ccccff' id='sign' onmousedown='shink()' align='center' style='cursor: hand; font-family: webdings'>5</td>/n" +
"<td width='90%' style='font-size:14px'>/n" +
"<p align='center'>{0}</td>/n" +
"</tr>/n" +
"</table>/n" +
"</td>/n" +
"</tr>/n" +
"<tr id='downblock'>/n" +
"<td width='228' height='113'>/n" +
"<table border='0' cellpadding='0' cellspacing='0' width='100%' height='106' style='font-size:14px'>/n" +
"<tr>/n" +
"<td width='100%' align='center' height='21' id='tr1'><a target='_blank' href='{6}'>{1}</a></td>/n" +
"</tr>/n" +
"<tr>/n" +
"<td width='100%' align='center' height='21' id='tr2'><a target='_blank' href='{7}'>{2}</a></td>/n" +
"</tr>/n" +
"<tr>/n" +
"<td width='100%' align='center' height='21' id='tr3'><a target='_blank' href='{8}'>{3}</a></td>/n" +
"</tr>/n" +
"<tr>/n" +
"<td width='100%' align='center' height='21' id='tr4'><a target='_blank' href='{9}'>{4}</a></td>/n" +
"</tr>/n" +
"<tr>/n" +
"<td width='100%' align='center' height='22' id='tr5'><a target='_blank' href='{10}'>{5}</a></td>/n" +
"</tr>/n" +
"</table>/n" +
"</td>/n" +
"</tr>/n" +
"</table>";
private const string javascript = "<script language='javascript'>/n" +
"var flag = true;/n" +
"function shink()/n" +
"{/n" +
"if(flag) /n" +
"{/n" +
"document.all.downblock.style.display = 'none';/n" +
"document.all.sign.innertext = '6';/n" +
"document.all.t.height = 20;/n" +
"} /n" +
"else/n" +
"{/n" +
"document.all.downblock.style.display = 'block';/n" +
"document.all.sign.innertext = '5';/n" +
"}/n" +
"flag = !flag;/n" +
"}/n" +
"</script>/n";
private stringitems _strings;
//注意下面红色的部分,一定要写哦
[
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 事件
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 成员
void istatemanager.trackviewstate()
{
base.trackviewstate();
}
bool istatemanager.istrackingviewstate
{
get
{
// todo: 添加 webcustomcontrol1.istrackingviewstate getter 实现
return false;
}
}
object istatemanager.saveviewstate()
{
// todo: 添加 webcustomcontrol1.saveviewstate 实现
return null;
}
void istatemanager.loadviewstate(object state)
{
// todo: 添加 webcustomcontrol1.loadviewstate 实现
}
#endregion
}
}
注册会员,创建你的web开发资料库,