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