【问题标题】:Providing Declarative data for controls为控件提供声明性数据
【发布时间】:2012-02-28 09:54:07
【问题描述】:

我正在学习服务器控件开发并寻找适当的说明以通过源以声明方式设置数据。例如,下拉列表控件提供如下声明性数据

<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; }
}

【问题讨论】:

    标签: c# asp.net menu custom-controls custom-server-controls


    【解决方案1】:

    你要找的答案在这里: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>
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2019-01-24
      • 2021-09-25
      • 1970-01-01
      • 2020-02-24
      相关资源
      最近更新 更多