looping through items in a repeater and fetching a dataitem field

A

adiel_g

I am trying to loop through a repeater to retrieve a dataitem field
but am getting a NullReferenceException.
I can find a checkbox control but cannot find a dataitem field. Here
is the code that is looping through the repeater:

Dim rc As RepeaterItemCollection = rptReport.Items
Dim ri As RepeaterItem
Dim chkSelected As System.Web.UI.WebControls.CheckBox
Dim customer As String

For Each ri In rc
chkSelected = CType(ri.FindControl("chkCaseSel"),
System.Web.UI.WebControls.CheckBox)
If chkSelected.Checked Then
customer = ri.DataItem("customer") ' this is
the error line
getCustomersSelected.Add(casenum) ' this just adds
the customer to an array list
End If
Next

As you can see the line failing is:

customer = ri.DataItem("customer") ' this is the error line

The error is "NullReferenceException was unhandled by user code"

Thanks Before Hand,
Adiel
 
A

adiel_g

Thanks, so how would I access the fields in the above loop without
using the DataItem property?

Thanks Again,
Adiel
 
E

Eliyahu Goldin

If you mean the fields of the datasource, you can get them only in the
ItemDataBound event that fires for every item separately.

If you mean the fields in the repeater, you don't need DataItem for them.
 
A

adiel_g

I think I understand what you mean. The datasource is not stored as
part of the repeater. So since it's not stored as part of the
repeater, the fields from the datasource would only be available while
the databinding is taking place (ItemDataBound). With this in mind
the workaround would be to hold the data needed into a portion of the
repeater that will remain after databinding. In another words holding
the data in a control defined inside the repeater control. One
example would be a label control. With this in mind, I set the data
inside a label and set the label's property to invisible:

<asp:label id="lblCustomer" runat="server" Visible="False" Text='<%#
DataBinder.Eval(Container.DataItem, "Customer") %>'></asp:label>

This solved my problem since now the field was available AFTER
databinding (ItemDataBound).

It is interesting how there is a dataitem property in the RepeaterItem
object:


Dim ri As RepeaterItem
....
customer = ri.DataItem("customer") ' object is there but data is not
there...


Of course this object is only available during binding time
(ItemDataBound) as described above by Eliyahu. So for now, a
workaround such as above must be used. If someone at Microsoft is
reading this, it would be nice for a future version of .NET if the
datasource would be available after the ItemDataBound. It looks like
they did not do this because of the overhead. But if overhead is the
issue, you can even make it an option during binding to include the
datasource in the repeater item or to not include it if you think its
an overhead. For example:


' Bind to repeater
rptReport.DataSource = dt
rptReport.IncludeDataSourceAfterBinding = True ' THIS WOULD
BE THE NEW PROPERTY: Would default to false as it works now...
rptReport.DataBind()


Thanks,
Adiel
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top