By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

ViewState?

P: n/a
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
<AlternatingItemTemplatearen'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

Aug 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

--
http://www.openmymind.net/
http://www.codebetter.com/
"Arpan" <ar******@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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
<AlternatingItemTemplatearen'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

Aug 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.