简体   繁体   English

多个 asp:Repeater DataBind

[英]Multiple asp:Repeater DataBind

I am 90% certain I have acheived this before, but I cannot remember how I did it.我 90% 确定我以前实现过这个,但我不记得我是怎么做到的。

I have a repeater which I would like to use twice on a page as the structure and databinding events are the same, but the data binding to the repeater is obviously different.我有一个转发器,我想在一个页面上使用它两次,因为结构和数据绑定事件是相同的,但是绑定到转发器的数据显然不同。

In the past I believe I set the datasource on the repeater then databinded, and then did the same again but with another datasource, so effectively:在过去,我相信我在转发器上设置了数据源,然后进行了数据绑定,然后再次使用另一个数据源进行了同样的操作,如此有效:

MyRepeater.DataSource = DataSourceOne;
MyRepeater.DataBind();
MyRepeater.DataSource = DataSourceTwo;
MyRepeater.DataBind();

Now this would have produced the html twice on the page.现在这将在页面上生成两次 html。 In this instance two lists, with different data contained inside of them.在本例中,有两个列表,其中包含不同的数据。

Thinking about it, it could possibly the type of datasource used.考虑一下,它可能是使用的数据源类型 Before it might of been a dataset/table I was binding to the repeater, but this time I am using an ArrayList.在它可能成为数据集/表之前,我绑定到转发器,但这次我使用的是 ArrayList。

ArrayList Items = new ArrayList();
Items = this.GetMenu(this._ProductsPageID);
this.rep_ProductsPortfolio.ItemDataBound += new RepeaterItemEventHandler(ProdPortItemDataBound);
this.rep_ProductsPortfolio.DataSource = Items;
this.rep_ProductsPortfolio.DataBind();

// Get portfolio
Items = this.GetMenu(this._PortfolioPageID);
this.rep_ProductsPortfolio.ItemDataBound += new RepeaterItemEventHandler(ProdPortItemDataBound);
this.rep_ProductsPortfolio.DataSource = Items;
this.rep_ProductsPortfolio.DataBind();

I have also tried using a different ArrayList for each repeater, but that didn't work either.我也尝试过为每个中继器使用不同的 ArrayList,但这也没有用。

At the moment all that happens is the second databind is rebinding over the old repeater and I only have one on the page.目前所发生的只是第二个数据绑定正在旧的中继器上重新绑定,而我在页面上只有一个。

Any ideas?有任何想法吗? Thanks in advance提前致谢

Are you sure in the past you didn't use two repeaters with one datasource?您确定过去没有对一个数据源使用两个中继器吗?

I think you need to use two repeaters here.我想你需要在这里使用两个中继器。

If you change the DataSource it owerrite the first one.如果您更改数据源,它会写入第一个。 My idea is to use two repeaters.我的想法是使用两个中继器。

Thanks everyone, I can't figure out how I did it before (if I even did,).谢谢大家,我不知道我以前是怎么做到的(如果我能做到的话)。 the time I wasted creating this question and now responding to it is ten-fold to the solution.我浪费在创建这个问题上的时间和现在回答它的时间是解决方案的十倍。

In the end I have just made it all work with one datasource with the binding events being handled, and as the only thing that is changing is the data being bound I simply copied the repeater and renamed it and wired the other datasource to the new repeater and re-used the databinding events, taking a whole 1 minute... haha最后,我只是让所有这些都与一个数据源一起工作,并处理了绑定事件,因为唯一改变的是绑定的数据,我只是复制了转发器并重命名它,并将另一个数据源连接到新的转发器并重新使用数据绑定事件,花了整整 1 分钟......哈哈

In short, can't be done, use two repeaters and one binding event handler简而言之,做不到,使用两个中继器一个绑定事件处理程序

Thanks anyway everyone: :)无论如何谢谢大家::)

I'm not sure about Dataset/tables, but I always used ArrayLists as DataSource of my Repeaters, and never got it duplicated.我不确定数据集/表,但我总是使用 ArrayLists 作为我的 Repeater 的数据源,并且从未复制过它。

But it seems unresonable the fact of one Repeater creating two tables in HTML code =O但是,一个 Repeater 在 HTML 代码中创建两个表的事实似乎是不合理的 =O

Anyway, setting the DataSource is replacing the reference and not "appending" it =)无论如何,设置 DataSource 是在替换引用而不是“附加”它 =)

Edit:编辑:

From MSDN documentation:来自 MSDN 文档:

Repeater.DataSource Property: Gets or sets the data source that provides data for populating the list. Repeater.DataSource 属性:获取或设置为填充列表提供数据的数据源。

As already mentioned, what you're doing will overwrite the output of the first databind.如前所述,您所做的将覆盖第一个数据绑定的输出。

But you could combine the lists before you databind them但是您可以在数据绑定之前组合列表

var allItems = new List<Foo>();
allItems.AddRange(fooDataSource1);
allItems.AddRange(fooDataSource2);
repeater1.Datsource = allItems;
repeater1.Databind();

Issue: Setting the DataSource is replacing the reference and not "appending"问题:设置数据源是替换引用而不是“追加”

Proposed Fix: What you can consider is use multiple DataTables and then Merge your DataTables before you set the Repeaters DataSource.建议的修复:您可以考虑使用多个 DataTable,然后在设置 Repeaters DataSource 之前合并您的 DataTable。 And thereafter you DataBind to the Repeater:)然后你将数据绑定到中继器:)

Steps : 1) Create 2 public DataTables步骤1) 创建 2 个公共数据表

public DataTable DT1 = new DataTable();
public DataTable DT2= new DataTable();

2) DataBind your data in DT1--> 2) DataBind 你在DT1中的数据-->

DBConnect con = new DBConnect();
DT1 = con.GetDT1();

Repeater1.DataSource = DT1;
Repeater1.DataBind();

3) DataBind your data in DT2 and merge into DT1 3)DataBind你在DT2中的数据并合并到DT1中

DBConnect con = new DBConnect();
DT2 = con.GetDT2Data();
DT1.Merge(DT2);

Repeater1.DataSource = DT1;
Repeater1.DataBind();

And there we go.我们开始了。 We have "Appended" our data.我们已经“附加”了我们的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM