ViewState?

A

Arpan

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",
sqlConn)

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

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

sqlConn.Close()
End If
End Sub

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

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

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.

Thanks,

Arpan
 
K

Karl Seguin [MVP]

Yes..you got everything right.

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

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

Karl
 

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

DataBind 1
DataBind In ItemDataBound Event 1
IsPostBack 4
OnItemCommand & OnItemDataBound 2
SqlDataAdapter & Label 0
DataGrid Edit Problem 9
DataList Change Row Color OnMouseOver 3
DropDownList DataGrid 1

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top