我有一个转发器生成LinkButtons
这是转发器:
<asp:Repeater runat="server" ID="rptBreadCrumb" >
<ItemTemplate>
<div>
<asp:LinkButton CssClass="process-breadcrumb" ID="lnkBrdCrmb" runat="server" OnCommand="lnkWeb_Click" />
</div>
</ItemTemplate>
</asp:Repeater>
这是代码隐藏代码:
var items = new List<object> {};
var urlList = weburl.ParseUrl();
foreach (var url in urlList)
{
using (var site = new SPSite(url))
{
using (var selectedWeb = site.OpenWeb())
{
items.Add(new { Text = selectedWeb.Title + " > ", Url = url });
}
}
}
rptBreadCrumb.DataSource = items;
rptBreadCrumb.DataBind();
我错过了什么吗?因为它没有显示任何文字。链接按钮生成,但没有文本。我怎样才能确保链接按钮得到它?
答案 0 :(得分:4)
您可以使用Repeaters
ItemDataBound
事件来设置网址和文字。
ASPX:
<asp:Repeater runat="server" ID="rptBreadCrumb" OnItemDataBound="rptBreadCrumb_DataBound" >
...
代码隐藏:
protected void rptBreadCrumb_DataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lnkBrdCrmb = (LinkButton)e.Item.FindControl("lnkBrdCrmb");
// get the anonymous type's properties via DataBinder.Eval
string text = (string)DataBinder.Eval(e.Item.DataItem, "Text");
string url = (string)DataBinder.Eval(e.Item.DataItem, "Url");
lnkBrdCrmb.Text = text;
lnkBrdCrmb.PostBackUrl = url;
}
}