[英]ASP.net simple repeater question
I'm trying to use the repeat without binding it to a datasource, is this possible? 我试图使用重复而不将其绑定到数据源,这可能吗?
<asp:Repeater runat="server" ID="rptPageNav">
<ItemTemplate>
<asp:HyperLink runat="server" CssClass="pageLink" ID="pageLink">#</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
Then in my code, I want to loop through adding the repeater item template for each link available. 然后在我的代码中,我想循环遍历为每个可用链接添加转发器项目模板。
for (int i = 0; i < thisTemplate.specification.pagination; i++)
{
}
So the end results should be something like: 因此,最终结果应类似于:
<a class="pageLink" href="#">1</a>
<a class="pageLink" href="#">2</a>
<a class="pageLink" href="#">3</a>
<a class="pageLink" href="#">4</a>
Alternatively, if you want only single simple link you don't need Repeater IMO you can have simple Panel then create the links on the fly: 或者,如果您只需要单个简单链接,则不需要Repeater IMO,则可以使用简单的Panel,然后动态创建链接:
for (int i = 1; i < 5; i++) {
HyperLink link = new HyperLink();
link.CssClass = "pageLink";
link.NavigateUrl = "#";
link.Text = i.ToString();
MyPanel.Controls.Add(link);
}
使用该值作为超链接的文本,创建一个所需的整数数组并将其绑定到转发器。
No, you have to bind a Repeater to some kind of datasource. 不,您必须将Repeater绑定到某种数据源。 Try using an array of ints as @Paddy suggests.
尝试使用@Paddy建议的整数数组。
Incidentally, you'll need to modify the markup within the <ItemTemplate>
tags to get your hyperlinks to display one above the other as in your example. 顺便说一句,您需要修改
<ItemTemplate>
标记内的标记,以使您的超链接显示为超链接,如示例所示。
MSDN defines asp:Repeater
as MSDN将as
asp:Repeater
定义为
A data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.
一个数据绑定列表控件,该控件允许通过对列表中显示的每个项目重复指定的模板来进行自定义布局。
Which essentially means we have to bind it to a datasource. 从本质上讲,这意味着我们必须将其绑定到数据源。 Why dont you try and implemt something like this
你为什么不尝试实现这样的事情
Markup 标记
<asp:Repeater runat="server" ID="rptPageNav">
<ItemTemplate>
<asp:HyperLink ID="pageLink" runat="server" CssClass="pageLink" NavigateUrl='<%# Eval("Link") %>'><%# Eval("Title") %></asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
Code 码
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<URL> urls = new List<URL>()
{
new URL(){ Link = "http://www.google.com", Title = "Google"},
new URL(){ Link = "http://www.yahoo.com", Title = "Yahoo"}
};
rptPageNav.DataSource = urls;
rptPageNav.DataBind();
}
}
}
public class URL
{
public string Link { get; set; }
public string Title { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.