Iteraiting through repeater

  • Thread starter Thread starter Marc Llenas
  • Start date Start date
M

Marc Llenas

Hi there,

I have a repeater that returns x rows from a main table.

I need to display some info stored on a secondary table that is linked to
the main using a one-to-many relationship.

Instead of using a nested repeater, I would like to manually do some checks
and display (or not) info contained on the secondary table.

Apparently the code to do this would be something like:

While objDataReader.Read()

'Do my checks and write whatever is needed

End While

Now if I place this code within the cell where the output is supposed to be
written (within the repeater), it returns nothing.

Am I doing something wrong? Is there a better way to do this?.

Thanks,

Marc
 
Marc,

You should use either ItemDataBound or PreRender event. In the ItemDataBound
event you get access to every item as it gets values from the database.
Based on this value, you can look into another table and modify the item
content. In the PreRender event all items are already fully built and you
can loop through them doing the same look up.

You should get the data from the second table inside a DataTable and look up
there, or, better use DataRelation class to link two tables.

Eliyahu
 
Thanks Eliyahu,
I implemented the ItemDataBound approach as you recommended.

Now, how do I retrieve a value from the repeater? I need to get the value of
an ID field on the repeater in order to use it as a filter for a query
against the DB.

I've tried storing that value on a hidden field and then retrieving the
field's value but doesn't work (returns nothing). Any ideas on how to solve
this?

Thanks and have a great weekend.

Marc
 
This should do what you want I think.

foreach (RepeaterItem dataItem in rptCart.Items)
{
CheckBox ckDelete =
(CheckBox)dataItem.FindControl("ckDel1");
TextBox txtLineNumber =
(TextBox)dataItem.FindControl("txtLineNumber");
TextBox txtQuantity =
(TextBox)dataItem.FindControl("txtQty");
int linenbr = int.Parse(txtLineNumber.Text);
if (ckDelete.Checked)
{
//delete the item
}
else
{
//update the item
}
}

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top