> Consider the following code which displays data in 2 columns in a
> Repeater (data in the 1st column are LinkButtons where as the data in
[quoted text clipped - 103 lines]
>
> Ron
Can someone please help me out with this?
Ron
Bear with me, C# coder trying to help with VB :)
<snip>
> Note the "If Not" condition in the sub Item_DataBound. If I am not
> wrong, the ItemDataBound event gets fired when the DataBind() method
> of a server control is invoked. Please correct me if I am wrong.
Yes, it does
> Now when a user comes to this page for the first time, it takes a long
> time for the above code to execute & finally the "Server Application
[quoted text clipped - 5 lines]
> in a never ending way (recursive call). Please correct me if I am
> wrong.
Yes, when debugging this will throw a StackOverflowException and die.
> Going by the same logic, if the "If" condition in the sub Item_Command
> is uncommented, then shouldn't that "If" condition also result in a
> recursive call to the Item_DataBound handler when the page posts back
> (when one of the LinkButtons is clicked in the Repeater) BUT that
> doesn't happen! In fact, the sub Item_DataBound doesn't get called
> only in the first place. Why?
Item_Command is only called after you click on a button in the repeater.
It won't get called recursively by calling DataBind in this case because
that won't trigger the Item_Command event.
> Moreover if the "If" condition in the sub Item_Command is uncommented,
> then when the page posts back (when one of the LinkButtons is clicked
> in the Repeater), the Repeater doesn't get rendered. Why?
Your repeater should be filled on PostBack from the view state. But, it
appears that the call to DataBind in your Item_Command is hosing the data.
Remove the DataBind's from your event handlers and you should be fine.
HTH.
RN1 - 06 Mar 2008 06:34 GMT
> Bear with me, C# coder trying to help with VB :)
>
[quoted text clipped - 37 lines]
>
> HTH.
>> Item_Command is only called after you click on a button in the repeater.
>> It won't get called recursively by calling DataBind in this case because
>> that won't trigger the Item_Command event.
No....I don't mean the Item_Command event handler getting called
recursively. I mean the Item_DataBound event handler getting called
recursively when a LinkButton is clicked in the Repeater & the page
posts back. This is how the Item_Command & Item_DataBound event
handlers look like:
Sub Item_Command(ByVal obj As Object, ByVal ea As
RepeaterCommandEventArgs)
Response.Write("Item Command<br>")
If (Page.IsPostBack) Then
rptrUsers.DataBind()
End If
End Sub
Sub Item_DataBound(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item DataBound<br>")
If (Page.IsPostBack) Then
rptrUsers.DataBind()
End If
End Sub
Now when a LinkButton in the Repeater is clicked, the page posts back.
Hence the "If" condition in the Item_Command event handler evaluates
to True. Since it evaluates to True, DataBind() in Item_Command is
called. Since DataBind() in Item_Command is called, the Item_DataBound
event handler is called.
Now since the page has posted back, the "If" condition in
Item_DataBound also evaluates to True. Since it evaluates to True,
DataBind() in Item_DataBound gets invoked. DataBind() in
Item_DataBound getting called means the Item_DataBound event handler
is called again. The "If" condition in Item_DataBound is True; so
DataBind() gets called again. This means Item_DataBound is called
again. So shouldn't it be a recursive call to Item_DataBound?
I know I am wrong but am getting confused.
>> But, it appears that the call to DataBind in your Item_Command is hosing the >> data.
What do you mean by "hosing the data"?
>> Remove the DataBind's from your event handlers and you should be fine
For a moment, let me jump to a DataGrid. Suppose I am using a DataGrid
which displays records from a database. I edit the DataGrid. The
OnEditCommand event handler of the DataGrid gets called. If I don't
invoke DataBind() in the OnEditCommand event handler of the DataGrid,
then after post back, the DataGrid won't reflect the change I made. Am
I correct? I guess I am.
If that's indeed the case, shouldn't the same logic apply to a
Repeater? But you have suggested to remove the DataBind() calls from
the Repeater's event handlers. Or is it because, unlike a DataGrid
where I am editing the DataGrid, a Repeater is read-only & hence on
post back, the content of the Repeater remains the same i.e. the
Repeater gets the data from the ViewState which is why you have
suggested to remove the DataBind calls from the Repeater's event
handlers?
I hope I have expressed my doubts lucidly!
Thanks,
Ron
Mufaka - 06 Mar 2008 07:09 GMT
>> Bear with me, C# coder trying to help with VB :)
>>
[quoted text clipped - 102 lines]
>
> Ron
In your original example, you had If Not (Page.IsPostBack) in the
Item_DataBound.
If you leave it that way, it will get called recursively because
Page_Load does a DataBind after a valid DataSource has been set (one
with items in it). So Item_DataBound will be called, which in turn does
a DataBind, which fires off the Item_DataBound event, which in
turn...just kidding, you get it. :P
If you make this If (Page.IsPostBack) it will *not* get called. That's
the tricky part. It's because when you do post back, your Page_Load will
not set a DataSource. That is only done when it's not a post back.
So, when your Item_Command is called, it is calling DataBind without a
DataSource. This clears (what I meant by hosing) the repeater items
because it's bound to nothing. If there are no items so Item_DataBound
won't be called.
The reason that you have If Not (Page.IsPostBack) in the Page_Load is to
prevent the repeater from being filled twice. Once from viewstate and
then once from your database call. If it's already filled, then there is
no reason to fetch it again from the db.
So, removing your DataBind calls from your event handlers should make
this work as you expect it to. There is no new or edited data to
consider for the repeater, so you don't need to worry about saving the
data and then reloading the control from updates that were posted back.