473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ViewState?

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
1 1491
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
10
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and...
1
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
7
by: et | last post by:
I'm not sure I understand the use of the ViewState. Do I understand correctly that values of controls are automatically held in a hidden control called ViewState? If so, then why can't we get...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
9
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
6
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
6
by: paul.hester | last post by:
Hi all, Does anyone know why the ViewState would be empty? When I'm receiving a postback, I can access a posted value using controlName.Value but not ViewState. I have EnableViewState set...
12
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.