为控件提供声明性数据
本文关键字:数据 声明 控件 | 更新日期: 2023-09-27 18:26:04
我正在学习服务器控制开发,并寻找适当的说明,以启用通过源声明设置数据。例如,dropdownlist控件提供声明性数据,如下面的
<asp:DropDownList id="dropdown" runat="server">
<asp:ListItem text="Project Initiation" value="1"></asp:ListItem>
<asp:ListItem text="Documentation" value="2"></asp:ListItem>
</asp:DropDownList>
类似地,我正在寻找以声明方式向我的简单菜单控件提供数据,该控件显示由数据源的图像包围的图像(输出标记)
<a>
<img src="" />
</a>
请不要给我指一个链接,至少任何人都可以这样做。我想清楚地解释一下提供这种功能需要做些什么。我希望我的最终来源看起来像下面的
<asp:sidebar runat="server" id="sb">
<asp:sidebaritem navigateurl="" imageurl="" label=""></asp:sidebaritem>
</asp:sidebar>
侧边栏项类定义如下,我也有一个属性
类
public class SidebarItem
{
private string _navigateUrl;
public string NavigateUrl
{
get { return _navigateUrl; }
set { _navigateUrl = value; }
}
. . .
}
财产
[PersistenceMode(PersistenceMode.InnerProperty)]
[NotifyParentProperty(true)]
public ICollection<SidebarItem> Items
{
get { return _sidebarItems; }
set { _sidebarItems = value; }
}
您正在寻找的答案如下:http://msdn.microsoft.com/en-us/library/9txe1d4x.aspx
由于您要求的不仅仅是一个链接,这里有一个特定于您的场景的示例解决方案:
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Items"),
ParseChildren(true, "Items"),
ToolboxData("<{0}:SideBar runat='"server'"> </{0}:SideBar>")
]
public class SideBar : WebControl
{
private ArrayList itemsList;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
//[Editor(typeof(ContactCollectionEditor), typeof(UITypeEditor))]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public ArrayList Items
{
get
{
if (itemsList == null)
{
itemsList = new ArrayList();
}
return itemsList;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
foreach (object o in itemsList)
{
SideBarItem item = (SideBarItem)o;
writer.RenderBeginTag(HtmlTextWriterTag.Li);
writer.AddAttribute(HtmlTextWriterAttribute.Href, item.NavigateUrl);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute(HtmlTextWriterAttribute.Src, item.ImageUrl);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag(); // Img
writer.RenderEndTag(); // A
writer.RenderEndTag(); // Li
}
writer.RenderEndTag(); // Ul
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class SideBarItem
{
public SideBarItem()
: this(String.Empty, String.Empty, String.Empty)
{
}
public SideBarItem(string imgUrl, string navUrl, string label)
{
ImageUrl = imgUrl;
NavigateUrl = navUrl;
Label = label;
}
[DefaultValue("")]
[NotifyParentProperty(true)]
public String ImageUrl { get; set; }
[DefaultValue("")]
[NotifyParentProperty(true)]
public String NavigateUrl { get; set; }
[DefaultValue("")]
[NotifyParentProperty(true)]
public String Label { get; set; }
}
然后在ASPX文件中如下所示:
参考
<%@ Register Assembly="MyProject" Namespace="MyProject" TagPrefix="cc1" %>
实施
<cc1:SideBar ID="SideBar1" runat="server">
<cc1:SideBarItem ImageUrl="#" NavigateUrl="#" />
<cc1:SideBarItem ImageUrl="#" NavigateUrl="#" />
</cc1:SideBar>