好吧,想象一下下一种情况:
我有一个带转发器的.ascx文件,在此转发器中有一个asp:hyperlink。
是否可以从后面的代码中设置此超链接的URL?
最后,链接将始终指向相同的URL,但需要重复x次。
测试代码ascx:
<asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<div class="container">
<asp:HyperLink ID="hypUrl" runat="server" Text="Dit is een test link"> </asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
通常,在后面的代码中,我会执行以下操作:
hypUrl.NavigateUrl = Url;
但是因为超链接位于转发器中,所以它似乎找不到ID。
任何人都知道实现此目标的最佳方法是什么?
提前致谢!
是的,在您的情况repeater_ItemDataBound
下:
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Find the hyperlink
HyperLink hypUrl = (HyperLink)e.Item.FindControl("hypUrl");
// Set the property
hypUrl.NavigateUrl = "foo";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句