The following code retrieves records from a database table & displays
it in a Repeater control:

<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("Data
Source=MyDS\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users",

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

rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"

End If
End Sub

Sub BindData(ByVal obj As Object, ByVal ea As
Response.Write("Data Bound<hr>")
End Sub

Sub ItemCreated(ByVal obj As Object, ByVal ea As
Response.Write("Item Created<br>")
End Sub
<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCreated="ItemCreated"
OnItemDataBound="BindData" runat="server">
<table border="1">
<th>PHONE NO.</th>
<asp:LinkButton ID="linkbut" runat="server"><%#
Container.DataItem("UName") %></asp:LinkButton>
<td><%# Container.DataItem("Phone") %></td>
<td><%# Container.DataItem("UName") %></td>
<td><%# Container.DataItem("Phone") %></td>

Note that the records under the "NAME" column within the <ItemTemplate>
are hyperlinks whereas the records under the "NAME" column within the
<AlternatingItemTemplate> aren't hyperlinks. Assume that the Repeater
displays 4 rows.

When the page loads for the very first time, both OnItemCreated &
OnItemDataBound get fired & the Repeater with the 4 rows also gets
rendered. Next I click the first hyperlink (whose index is 0). When the
page re-loads, only OnItemCreated gets fired. Now although the entire
code in the Page_Load sub (which creates & opens a DB connection,
retrieves the records, fills the DataSet & finally spits out the
resultset in the Repeater control) is within the "If Not
(Page.IsPostBack)" condition, the Repeater still displays the 4 rows
when the page re-loads.

Now during this postback, the Repeater with the 4 rows gets recreated
from the ViewState, isn't it? This is what I did like to make sure of
i.e. whether the Repeater gets recreated from the ViewState or not?
Please correct me if I am wrong.





Karl Seguin [MVP] got everything right.

It's also why you are seeing ItemCreated fire on postback and not

The Items are still being created (from viewstate), but they aren't being
bound via a call to DataBound().


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