"Stopping" A DataList

  • Thread starter Thread starter Barrie Wilson
  • Start date Start date
B

Barrie Wilson

How can I arbitrarily terminate iteration in a DataList or Repeater?

I'm thinking along these lines:

protected void DataList1_OnItemDataBound(object sender,
DataListItemEventArgs e)
{
myRowCount++;

if (myRowCount > 200)
{
// cancel the iteration ... but how?
}
}

TIA
BW
 
Peter Bromberg said:
Well, the eventhandler returns void, so have you tried:

if (myRowCount > 200)
{

return ;
}

no, that doesn't stop the iteration ..
--Actually I think it might be a lot cleaner if you just modified the
datasource prior to databinding.

I'm not clear what you mean here; modify it how and when? before binding
the DataList or before binding the row? suppose I only want to return N
rows contingent upon some arbitrary condition ? how would I modify the
datasource in mid-stream? suppose, for example, we display no more data if
an accumulator exceeds a certain value ...

in any case, in this instance the datasource is a method which returns an
array : DirectoryInfo.GetFiles() ... so I don't know how I would modify it
in any meaningful way ... I suppose there's a way to essentially put some
smarts into that method .... but really I was interested in knowing whether
there was simply a way to terminate iteration ...

thanks for your reply ..
BW
 
Don't do things in mid-stream. Use PreRender event when the datalist has
been already fully built. At this stage you can hide unwanted items with
setting their Visible property to false. It will have the same effect as
deleting them from the datalist since they won't be rendered to the client.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
Don't do things in mid-stream. Use PreRender event when the datalist has
been already fully built. At this stage you can hide unwanted items with
setting their Visible property to false. It will have the same effect as
deleting them from the datalist since they won't be rendered to the
client.

thanks for the reply .. but that's the kind of hack I prefer to avoid if
possible

you've never terminated a loop at some point in its iteration? that what
the "break" statement is for, no? I'm just interested in knowing if the
Framework designers exposed the ability to do so with high-level iterative
controls like Repeater and DataList ... personally, I think they should have
.... I don't particularly like the idea of iteration being exclusively
determined by the characteristics of another object ... but that's just my
two cents ..

I expected to see something like this possibility in the dataBound handler:

e.CancelIteration="true";

guess not .... I suppose I could write my own Repeater control ... but I'd
rather not ....

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


Barrie Wilson said:
no, that doesn't stop the iteration ..


I'm not clear what you mean here; modify it how and when? before binding
the DataList or before binding the row? suppose I only want to return N
rows contingent upon some arbitrary condition ? how would I modify the
datasource in mid-stream? suppose, for example, we display no more data
if an accumulator exceeds a certain value ...

in any case, in this instance the datasource is a method which returns an
array : DirectoryInfo.GetFiles() ... so I don't know how I would modify
it in any meaningful way ... I suppose there's a way to essentially put
some smarts into that method .... but really I was interested in knowing
whether there was simply a way to terminate iteration ...

thanks for your reply ..
BW
 
It is as hack, as any other of thousands of little tricks that any web
programmer has to employ. Anyway...

Perhaps what is confusing you is the fact that ItemDataBound event fires for
every item. If your datasource has 400 items, the event will fire 400 times
(or more if you have a header, a footer etc). Whatever you are doing in the
event handler for item #200 won't stop the event from firing for item #201.
Like Peter said, you can put a return statement in the beginning of the
handler but the proper way is to organize your datasource before
databinding. For example, if your data source is a DataTable, you may
consider making a DataView including only rows you need to show.


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


Barrie Wilson said:
Don't do things in mid-stream. Use PreRender event when the datalist has
been already fully built. At this stage you can hide unwanted items with
setting their Visible property to false. It will have the same effect as
deleting them from the datalist since they won't be rendered to the
client.

thanks for the reply .. but that's the kind of hack I prefer to avoid if
possible

you've never terminated a loop at some point in its iteration? that what
the "break" statement is for, no? I'm just interested in knowing if the
Framework designers exposed the ability to do so with high-level iterative
controls like Repeater and DataList ... personally, I think they should
have ... I don't particularly like the idea of iteration being exclusively
determined by the characteristics of another object ... but that's just my
two cents ..

I expected to see something like this possibility in the dataBound
handler:

e.CancelIteration="true";

guess not .... I suppose I could write my own Repeater control ... but
I'd rather not ....

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


Barrie Wilson said:
message Well, the eventhandler returns void, so have you tried:

if (myRowCount > 200)
{

return ;
}


no, that doesn't stop the iteration ..

--Actually I think it might be a lot cleaner if you just modified the
datasource prior to databinding.

I'm not clear what you mean here; modify it how and when? before
binding the DataList or before binding the row? suppose I only want to
return N rows contingent upon some arbitrary condition ? how would I
modify the datasource in mid-stream? suppose, for example, we display
no more data if an accumulator exceeds a certain value ...

in any case, in this instance the datasource is a method which returns
an array : DirectoryInfo.GetFiles() ... so I don't know how I would
modify it in any meaningful way ... I suppose there's a way to
essentially put some smarts into that method .... but really I was
interested in knowing whether there was simply a way to terminate
iteration ...

thanks for your reply ..
BW
 
It is as hack, as any other of thousands of little tricks that any web
programmer has to employ. Anyway...
Perhaps what is confusing you is the fact that ItemDataBound event fires
for every item.

I'm not confused at all; I understand the default behavior of the DataList
and Repeater controls re iteration of the data source. My question was
whether either control provided a means of terminating iteration, which is
quite a normal thing to do in a loop

If it can't be done, well, it can't be done; I just want to understand the
limitations of the Framework ... personally, I think you should be able to
do it but this would not be the first nor the last instance of a
paternalistic design philosophy from MS
If your datasource has 400 items, the event will fire 400 times (or more
if you have a header, a footer etc). Whatever you are doing in the event
handler for item #200 won't stop the event from firing for item #201.

it certainly would stop the event if there were a means of terminating the
iteration since the 201st item would not be read at all if you didn't want
it to be read
Like Peter said, you can put a return statement in the beginning of the
handler but the proper way is to organize your datasource before
databinding.

putting a return statement in has no effect at all; it's equivalent to
putting no code in the handler at all; I'm not sure I'd consider
"organizing your datasource before databinding" the "proper" way handle the
issue; it may well the the ONLY way to handle it but "proper" carries other
connotations which I don't think apply under the circumstances
 
Back
Top