如何在c#.net中使用中继器 [英] how to use repeater in c#.net

查看:63
本文介绍了如何在c#.net中使用中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用转发器,您是否可以使用example

i dont know how to use repeater could u give some reference with example

推荐答案

给出一些参考?我要假设您的意思是ASP.NET Repeater控件.

转发器完全按照其名称的含义进行操作:它为数据绑定到的每个项目重复一个模板. (可选)您还可以指定分别在重复项之前和之后出现的页眉和/或页脚模板.

本质上,它们是HTML和/或其他简单ASP.NET控件的模板.使用中继器是一个两步过程.首先,将一个添加到页面并定义模板.让我们做一个简单的:

I''m going to assume you mean the ASP.NET Repeater control.

The repeater does exactly what its name suggests: it repeats a template for each item it is databound to. Optionally, you can also specify a header and/or footer template to appear before and after, respectively, the repeated items.

Essentially they are a template of HTML and/or other simple ASP.NET controls. Using a repeater is a two-step process. First, add one to your page and define the templates. Let''s do an easy one:

<asp:Repeater ID="myRepeater" runat="server">
    <HeaderTemplate>
        <div style="font-weight: bold;">My Repeater</div>
    </HeaderTemplate>
    <ItemTemplate>
        <div><asp:Label ID="myLabel" runat="server"/></div>
    </ItemTemplate>
</asp:Repeater>



没什么值得一看的,但是易于遵循.这将为每个重复的项目输出一个div,其中包含一个简单的ASP.NET标签. div很简单,因此当您在浏览器中看到div时,它们会将每行放在一个新行中.另外,我已经定义了HeaderTemplate并在其中放置一些粗体文本,这些文本将首先显示在我的标签行上方.现在,标签是空的,因此我们需要再做一步:将数据绑定到某物!

您可以在代码隐藏中使用SqlDataSourceObjectDataSource,甚至只是一个简单的List<string>对象.我们这样做,因为它不需要数据库连接或任何花哨的东西.



Not much to look at, but easy to follow. This will output a div containing a simple ASP.NET Label for each item it repeats over. The div is simply so that it puts each on a new line when you see it in the browser. Additionally, I''ve defined a HeaderTemplate and put some bold text there, which will appear first and above my lines of Labels. Right now the Labels are empty though, so we need to do one more step: databind to something!

You can use a SqlDataSource, ObjectDataSource, or even just a simple List<string> object in your code-behind. Let''s do that, since it doesn''t require a database connection or anything fancy.

private void Page_Load(object sender, EventArgs e)
{
    List<string> myList = new List<string>();
    myList.Add("Test1");
    myList.Add("Test2");
    myList.Add("Test3");

    myRepeater.DataSource = myList;

    myRepeater.DataBind();
}



我创建了一个由三个字符串组成的简单列表,并将中继器绑定到该字符串.

现在我们要显示一些数据,我们需要再更改一件事.我们需要将标签链接到数据,以便它们显示内容.更改ItemTemplate中的Label控件,如下所示:



I''ve created a simple list of three strings and had the repeater bind to that.

Now that we have some data to display, we need to change one more thing. We need to link the labels to the data so that they''ll display the contents. Change the Label control in the ItemTemplate to look like this:

<asp:Label ID="myLabel" runat="server" Text=''<%# Container.DataItem %>''/>



这使用数据绑定表达式来获取当前项目并将其注入标签的text属性中.由于我们要绑定到字符串列表,因此DataItem将仅仅是一个字符串.表达式还有更多内容,但我建议您单独阅读这些内容,因为它们超出了本文的范围.

如果运行此命令,则应在浏览器中看到类似以下内容的内容:



This uses databinding expressions to get the current item and inject it into the text property of the label. Since we''re binding to a list of strings, DataItem will be simply a string. There''s much more to expressions, but I suggest you read up on those separately as they are beyond the scope of this.

If you run this, you should see something like the following in your browser:

我的中继器


Test1


Test2


Test3
Test3


希望中继器控件 [ ^ ]将有所帮助您.
Hope Repeater Control[^] will help you.


这篇关于如何在c#.net中使用中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆