473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsPostBac k) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(" Data
Source=MyDS\SQL EXPRESS;Initial Catalog=MyDB;In tegrated Security=True")
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users",
sqlConn)

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

rptrUsers.DataS ource = dSet
rptrUsers.DataM ember = "Users"
rptrUsers.DataB ind()

sqlConn.Close()
End If
End Sub

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

Sub ItemCreated(ByV al obj As Object, ByVal ea As
RepeaterItemEve ntArgs)
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:LinkButt on ID="linkbut" runat="server"> <%#
Container.DataI tem("UName") %></asp:LinkButton>
</td>
<td><%# Container.DataI tem("Phone") %></td>
</tr>
</ItemTemplate>
<AlternatingIte mTemplate>
<tr>
<td><%# Container.DataI tem("UName") %></td>
<td><%# Container.DataI tem("Phone") %></td>
</tr>
</AlternatingItem Template>
<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
<AlternatingIte mTemplatearen'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.IsPostBac k)" 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 1499
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******@hotma il.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.IsPostBac k) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(" Data
Source=MyDS\SQL EXPRESS;Initial Catalog=MyDB;In tegrated Security=True")
sqlDapter = New SqlDataAdapter( "SELECT * FROM Users",
sqlConn)

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

rptrUsers.DataS ource = dSet
rptrUsers.DataM ember = "Users"
rptrUsers.DataB ind()

sqlConn.Close()
End If
End Sub

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

Sub ItemCreated(ByV al obj As Object, ByVal ea As
RepeaterItemEve ntArgs)
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:LinkButt on ID="linkbut" runat="server"> <%#
Container.DataI tem("UName") %></asp:LinkButton>
</td>
<td><%# Container.DataI tem("Phone") %></td>
</tr>
</ItemTemplate>
<AlternatingIte mTemplate>
<tr>
<td><%# Container.DataI tem("UName") %></td>
<td><%# Container.DataI tem("Phone") %></td>
</tr>
</AlternatingItem Template>
<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
<AlternatingIte mTemplatearen'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.IsPostBac k)" 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
21631
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 the results in the grid. Say you filter the grid for records that have a certain condition set to "NO" (in this case a checkbox). In this scenario...
3
2634
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 and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created...
10
2247
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 disable the session state in Web.Config the VIEWSTATE variable is still maintained and stores some values. Can anyone tell what those values are for,...
1
1761
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 properties declared on it over and above that provided by the textbox. As I understand it, part of the process to make this work is, in the property...
7
2035
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 them, or how do we get that value? I always have to set the ViewState("FirstName") = txtBox.text myself before a postback is done, otherwise, the...
3
4554
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 set textboxes, radio buttons, etc - that viewstate is fine. Then I have a linkbutton that does a Server.Transfer over to Page2.aspx. When I...
9
1856
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 the server to remember a variables state *without* having to go through the rigmarole of saving and loading to and from the Session state manually or...
6
2058
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 page gets post back it gives following error Failed to load viewstate. The control tree into which viewstate is
6
4445
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 to true in my master page and content page. Thanks,
12
1904
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
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.