简体   繁体   中英

Multiple asp:Repeater DataBind

I am 90% certain I have acheived this before, but I cannot remember how I did it.

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. 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 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.

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

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.

But it seems unresonable the fact of one Repeater creating two tables in HTML code =O

Anyway, setting the DataSource is replacing the reference and not "appending" it =)

Edit:

From MSDN documentation:

Repeater.DataSource Property: the data source that provides data for populating the list. 为填充列表提供数据的数据源。

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. And thereafter you DataBind to the Repeater:)

Steps : 1) Create 2 public DataTables

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

2) DataBind your data in DT1-->

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

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

3) DataBind your data in DT2 and merge into 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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