自定义控件代码如下
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextLink.ascx.cs" Inherits="PoEB.Web.Controls.TextLink" %> <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
后台代码
public partial class TextLink : System.Web.UI.UserControl { /// <summary> /// 商品调用位ID /// </summary> [Browsable(true), Description("商品调用位ID")] public int SpaceID { get; set; } /// <summary> /// 显示数量 /// </summary> [Browsable(true), Description("显示数量")] public int Number { get; set; } private ITemplate _template = null; [TemplateContainer(typeof(ItemContainer))] [Description("链接模板")] [PersistenceMode(PersistenceMode.InnerDefaultProperty)] public ITemplate Template { get { return _template; } set { _template = value; } } protected void Page_Load(object sender, EventArgs e) { DataBind(); } void Page_Init() { if (Template != null && SpaceID > 0 && Number > 0) { BLL.WebSpaceLink bllWebSpaceLink = new BLL.WebSpaceLink(); List<Model.WebSpaceLink> list = bllWebSpaceLink.GetListFromCache(SpaceID, Number); if (list != null) { int i = 0; foreach (Model.WebSpaceLink model in list) { ItemContainer container = new ItemContainer(i, model.LinkText,model.LinkUrl,model.Target); _template.InstantiateIn(container); PlaceHolder1.Controls.Add(container); i++; } } } } public class ItemContainer : Control, INamingContainer { private int _index; private string _text; private string _url; private string _target; internal ItemContainer(int index, string text,string url,string target) { _index = index; _text = text; _url = url; _target = target; } /// <summary> /// 友情链接ID /// </summary> [Description("链接文本")] [Bindable(true)] public string LinkText { get { return _text; } } /// <summary> /// 网站名称 /// </summary> [Description("链接网址")] [Bindable(true)] public string LinkUrl { get { return _url; } } /// <summary> /// 打开窗口类型 /// </summary> [Description("打开窗口类型")] [Bindable(true)] public string Target { get { return _target; } } /// <summary> /// 数据索引顺序. /// </summary> [Description("数据索引顺序")] [Bindable(true)] public int ItemIndex { get { return _index; } } } }
调用页面
<%@ Register TagPrefix="uc2" TagName="TextLink" Src="~/Controls/TextLink.ascx" %> <uc2:TextLink runat="server" ID="txtlk" SpaceID="102" Number="10"> <Template> <%--这里该如何写--%> </Template> </uc2:TextLink>
<%# Container.LinkText%>
要用自定义控件吧。
把这文件直接拉到要使用的页面就可以
问题看清楚再回答好不