更改 Repeater 控件中的 ASP.NET 控件的属性
我的问题很简单。 这就是我对 aspx 页面的内容:
<ul>
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" /></li>
</ItemTemplate>
</asp:Repeater>
</ul>
我正在尝试将超链接列表从 SQL 服务器获取到列表中。 这就是我在代码隐藏中的内容:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = Utilities.RunSelectQuery("SELECT *");
DataTable dt = ds.Tables[0];
linksList.DataSource = dt;
linksList.DataBind();
}
数据绑定到后,如何更改 asp:HyperLink
中的 NavigateUrl
和 Text
属性中继器
? 我想在代码隐藏中执行此操作,如果我在 aspx 页面中使用 <%# Eval("URL") %>
来执行此操作,我可以让它工作,但这有点违背ASP.NET 就是这样。
编辑:由于 womp,这是对我有用的解决方案:
protected void linksList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
HyperLink link = (HyperLink)e.Item.FindControl("link");
link.Text = row["description"].ToString();
link.NavigateUrl = row["URL"].ToString();
}
}
My question is fairly simple. This is what I have for the aspx page:
<ul>
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" /></li>
</ItemTemplate>
</asp:Repeater>
</ul>
I'm trying to get a list of hyperlinks from a SQL server into a list. This is what I have in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = Utilities.RunSelectQuery("SELECT *");
DataTable dt = ds.Tables[0];
linksList.DataSource = dt;
linksList.DataBind();
}
How do I change the NavigateUrl
and Text
properties in the asp:HyperLink
after data's been bound to the Repeater
? I want to do this in the codebehind, I can get it to work if I do it using <%# Eval("URL") %>
in the aspx page but that's sort of against what ASP.NET is all about.
Edit: this is the solution that worked for me thanks to womp:
protected void linksList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
HyperLink link = (HyperLink)e.Item.FindControl("link");
link.Text = row["description"].ToString();
link.NavigateUrl = row["URL"].ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,在模板中使用 Databinder 语法 是一个很好的方法这样做,我不确定你的意思是“违背了 ASP.Net 的全部内容”。
但是,如果您确实想在代码中执行此操作,则可以直接在 OnItemDataBound 处理程序(看起来像是您创建的)中执行此操作。 像这样的东西(阅读:未经测试)应该可以解决问题:
Actually, using the Databinder syntax in your templates is a great way to do it, I'm not sure what you mean that it's "against what ASP.Net is all about".
However, if you really want to do it in code, you can do it right in your OnItemDataBound handler (which it looks like you've created). Something like this (read: untested) should do the trick:
实现的方法就像你说的那样。 如果您只想显示 URL,则在后面的代码中执行此操作会增加不必要的工作。 您还需要将标签放入 HeaderTemplate 和 FooterTemplate 中。
The way to do it is just as you said. Doing it in the code behind adds unnecessary work if you only wish to display the URL. You also would want to put the tags in the HeaderTemplate and FooterTemplate.