IsPostBack

R

RN1

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
the 2nd column is just plain text):

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection("........")
sqlDapter = New SqlDataAdapter("SELECT * FROM tblUsers",
sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"
rptrUsers.DataBind()

sqlConn.Close()
End If
End Sub

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 Not (Page.IsPostBack) Then
rptrUsers.DataBind()
End If
End Sub

Sub Item_Created(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item Created<br>")
End Sub
</script>

<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCommand="Item_Command"
OnItemCreated="Item_Created" OnItemDataBound="Item_DataBound"
runat="server">

<HeaderTemplate>
<table border="1">
<tr>
<th>NAME</th>
<th>PHONE</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkName" CommandArgument='<%#
Container.DataItem("LastName") %>' CommandName='<%#
Container.DataItem("FirstName") %>' Text='<%#
Container.DataItem("FirstName") & " " & Container.DataItem("LastName")
%>' runat="server"></asp:LinkButton>
</td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

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.

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
Unavailable" error gets generated (may be the script times out).

If I am not mistaken, this happens because the call to the DataBind()
method in the sub Item_DataBound will invoke the Item_DataBound
handler which in turn will call DataBind() again & this will continue
in a never ending way (recursive call). Please correct me if I am
wrong.

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?

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?

Ron
 
R

RN1

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
the 2nd column is just plain text):

<script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
        If Not (Page.IsPostBack) Then
            Dim dSet As DataSet
            Dim sqlConn As SqlConnection
            Dim sqlDapter As SqlDataAdapter

            sqlConn = New SqlConnection("........")
            sqlDapter = New SqlDataAdapter("SELECT * FROM tblUsers",
sqlConn)

            dSet = New DataSet()
            sqlDapter.Fill(dSet, "Users")

            rptrUsers.DataSource = dSet
            rptrUsers.DataMember = "Users"
            rptrUsers.DataBind()

            sqlConn.Close()
        End If
    End Sub

    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 Not (Page.IsPostBack) Then
            rptrUsers.DataBind()
        End If
    End Sub

    Sub Item_Created(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
        Response.Write("Item Created<br>")
    End Sub
</script>

<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCommand="Item_Command"
OnItemCreated="Item_Created" OnItemDataBound="Item_DataBound"
runat="server">

<HeaderTemplate>
<table border="1">
<tr>
<th>NAME</th>
<th>PHONE</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkName" CommandArgument='<%#
Container.DataItem("LastName") %>' CommandName='<%#
Container.DataItem("FirstName") %>' Text='<%#
Container.DataItem("FirstName") & " " & Container.DataItem("LastName")
%>' runat="server"></asp:LinkButton>
</td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

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.

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
Unavailable" error gets generated (may be the script times out).

If I am not mistaken, this happens because the call to the DataBind()
method in the sub Item_DataBound will invoke the Item_DataBound
handler which in turn will call DataBind() again & this will continue
in a never ending way (recursive call). Please correct me if I am
wrong.

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?

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?

Ron

Can someone please help me out with this?

Ron
 
M

Mufaka

Bear with me, C# coder trying to help with VB :)

RN1 wrote:
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
Unavailable" error gets generated (may be the script times out).

If I am not mistaken, this happens because the call to the DataBind()
method in the sub Item_DataBound will invoke the Item_DataBound
handler which in turn will call DataBind() again & this will continue
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.
 
R

RN1

Bear with me, C# coder trying to help with VB :)

RN1 wrote:



Yes, it does



Yes, when debugging this will throw a StackOverflowException and die.


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.


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.

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.

What do you mean by "hosing the data"?

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
 
M

Mufaka

RN1 said:
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.


What do you mean by "hosing the data"?


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
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.
 

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

Similar Threads

Repeater OnItemCommand 1
DataBind 1
DataBind In ItemDataBound Event 1
ViewState? 1
OnItemCommand & OnItemDataBound 2
DropDownList DataGrid 1
DataGrid Edit Problem 9
Dynamic BoundColumn 2

Top