looping through items in a repeater and fetching a dataitem field

looping through items in a repeater and fetching a dataitem field

am 09.01.2008 17:11:41 von 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

Re: looping through items in a repeater and fetching a dataitem field

am 09.01.2008 17:22:26 von Eliyahu Goldin

DataItem is available only in the ItemDataBound event. Makes perfect sense
if you think of it since after the ItemDataBound items don't need anything
from the datasource, all data are already inside the items.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


wrote in message
news:2d3c6a2b-f976-458f-9983-86df6339c9bb@l1g2000hsa.googleg roups.com...
>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

Re: looping through items in a repeater and fetching a dataitem field

am 09.01.2008 17:33:10 von adiel_g

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

Thanks Again,
Adiel

>>DataItem is available only in the ItemDataBound event. Makes perfect sense
>>if you think of it since after the ItemDataBound items don't need anything
>>from the datasource, all data are already inside the items.

Re: looping through items in a repeater and fetching a dataitem field

am 09.01.2008 18:48:13 von 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.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


wrote in message
news:c8ac2d7f-db6e-4dc7-ae71-d7dc2b95c8b6@q39g2000hsf.google groups.com...
> Thanks, so how would I access the fields in the above loop without
> using the DataItem property?
>
> Thanks Again,
> Adiel
>
> >>DataItem is available only in the ItemDataBound event. Makes perfect
sense
> >>if you think of it since after the ItemDataBound items don't need
anything
> >>from the datasource, all data are already inside the items.

Re: looping through items in a repeater and fetching a dataitem field

am 10.01.2008 16:24:45 von 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:



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