Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

IsPostBack

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 05 Mar 2008 22:20 GMT
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
RN1 - 06 Mar 2008 02:40 GMT
> 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
Mufaka - 06 Mar 2008 04:46 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.