473,748 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ViewState and Repeater Control

Hello all,

I am experimenting with the repeater control and ran into something
that I wasn't expecting. I would appreciate if the experts can confirm
or correct my understanding.

Here is a fragment of a very simple page that I wrote that will drill
down into the displayed item. The result is to be display on the same
page so that the user can keep on drilling down:

<asp:Repeater id="rptCtrl1" runat="server" EnableViewState ="true" >
<ItemTemplate >
<asp:LinkButt on ID="lnkItem" Runat="server"
EnableViewState ="true" CommandName="Dr illDown"></asp:LinkButton>
<br>
</ItemTemplate>
</asp:Repeater>

The OnItemCommand event for the Repeater is connected to the
rptCtrl1_OnItem Command function:

public void rptCtrl1_OnItem Command(object source,
System.Web.UI.W ebControls.Repe aterCommandEven tArgs e)
{
LinkButton lnkButton = (LinkButton)e.C ommandSource;
string param = lnkButton.Comma ndArgument.ToSt ring();
makeDataBind(pa ram);
}
The makeDataBind function will perform the data binding depends on the
link that was clicked. All is fine when the EnableViewState was set to
true for both the repeater and the link button.

When the EnableViewState of the Repeater control is set to false, the
OnItemCommand event doesn't fire;
When the EnableViewState of the Linkbutton is set to false, the
LinkButton is not found in the OnItemCommand event.

I understand that the ViewState is responsible for recreating the
controls on the page. Without the ViewState, the link button was not
recreated so I couldn't get the link button during the event.

But why didn't the Repeater's OnItemCommand event fire when the
ViewState was disabled? If it was a button, the OnClick event will
still fire if the ViewState for the control is set to false.

Is it because the link button won't be recreated if the Repeater's
ViewState was disabled? Without the link button, there was no event to
bubble up?

One other question: With the code above, I can drill down to level 1
from level 0. If I press the back button from level 1, I go back to
level 0. But if I press the back button from level 2, I get a page
cannot be displayed error. If I press the back button again, I go back
to level 0.

I look forward to any advise and suggestions. Thanks in advance.

-Amos

Nov 19 '05 #1
1 5445
Yes, the control needs to be recreated on postback for the event to fire.
You had stated when you disable the ViewState on an asp:Button the click
event still fires.

<asp:Button id="myButton" runat="server" EnableViewState ="False" />

That is correct as on the postback, when the page (aspx) is parsed, there is
a button control called myButton that is recreated. Since there is a button
the page can fire the event for the button.

Inside a repeater control you have
<asp:LinkButt on ID="lnkItem" Runat="server" EnableViewState ="true" />,
but if you were too look at the client side name is something like
"rptCtrl1_ctl0_ lnkItem". Without viewstate enabled on the repeater, the
repeater control can't recreate all the linkItems that were rendered. So
when it comes time to fire the event, the framework can not find a control
called "rptCtrl1_ctl0_ lnkItem", so the event doesn't get fired.

The main difference, as I understand your question to be, is there is a
button control created everytime on the each postback without viewstate as
the control is clearly defined on the page. When using a repeater control,
there is no control created called rptCtrl1_ctl0_l nkItem without the
repeater recreating the control, or you binding the data again on each
postback.

HTH,

bill
<ol**********@y ahoox.com> wrote in message
news:lq******** *************** *********@4ax.c om...
Hello all,

I am experimenting with the repeater control and ran into something
that I wasn't expecting. I would appreciate if the experts can confirm
or correct my understanding.

Here is a fragment of a very simple page that I wrote that will drill
down into the displayed item. The result is to be display on the same
page so that the user can keep on drilling down:

<asp:Repeater id="rptCtrl1" runat="server" EnableViewState ="true" >
<ItemTemplate >
<asp:LinkButt on ID="lnkItem" Runat="server"
EnableViewState ="true" CommandName="Dr illDown"></asp:LinkButton>
<br>
</ItemTemplate>
</asp:Repeater>

The OnItemCommand event for the Repeater is connected to the
rptCtrl1_OnItem Command function:

public void rptCtrl1_OnItem Command(object source,
System.Web.UI.W ebControls.Repe aterCommandEven tArgs e)
{
LinkButton lnkButton = (LinkButton)e.C ommandSource;
string param = lnkButton.Comma ndArgument.ToSt ring();
makeDataBind(pa ram);
}
The makeDataBind function will perform the data binding depends on the
link that was clicked. All is fine when the EnableViewState was set to
true for both the repeater and the link button.

When the EnableViewState of the Repeater control is set to false, the
OnItemCommand event doesn't fire;
When the EnableViewState of the Linkbutton is set to false, the
LinkButton is not found in the OnItemCommand event.

I understand that the ViewState is responsible for recreating the
controls on the page. Without the ViewState, the link button was not
recreated so I couldn't get the link button during the event.

But why didn't the Repeater's OnItemCommand event fire when the
ViewState was disabled? If it was a button, the OnClick event will
still fire if the ViewState for the control is set to false.

Is it because the link button won't be recreated if the Repeater's
ViewState was disabled? Without the link button, there was no event to
bubble up?

One other question: With the code above, I can drill down to level 1
from level 0. If I press the back button from level 1, I go back to
level 0. But if I press the back button from level 2, I get a page
cannot be displayed error. If I press the back button again, I go back
to level 0.

I look forward to any advise and suggestions. Thanks in advance.

-Amos

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4279
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
0
2117
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
4
9001
by: Harry | last post by:
Hello, I have a Repeater control that contains a Label control inside its ItemTemplate. A dummy mockup of the HTML code looks as follows: <asp:repeater id="StyleRepeater" Runat="server"> <ItemTemplate> <asp:Label ID="ClassLabel" Runat="server"> <... various other ASP.NET Web Controls ...> </ItemTemplate> </asp:repeater>
2
1645
by: Amir | last post by:
Hi everyone How can I expand the size of an aspx page's viewstate or all pages' viewstate? thanks in advanced
8
1334
by: guoqi zheng | last post by:
Viewstate is nice, but you know viewstate data counts 1/3 of my page size. I need viewstate because I have button control inside my repeater control, but any idea how can I reduce the viewstate site, it sounds horrible to have so big viewstate. regards, Guoqi Zheng http://www.ureader.com
1
1430
by: Hardy Wang | last post by:
Hi all, I have a main ASPX page, and a control ASCX page, there is a Repeater control in ASCX page, and several drop down boxes in main page. These drop down boxes will trigger postback. In the ASCX page, if I put code like private void Page_Load(object sender, System.EventArgs e) { if (! Page.IsPostBack) { Data_Binding(); // to populate values in Repeater } }
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
1
1514
by: Arpan | last post by:
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
0
1898
by: Dave A | last post by:
I have a user control that is dynamically loaded into a repeater; (so the user control appears several times) The user control features a text box and a delete button. When the delete button is clicked the usercontrol raises an event indicating that the data in the database that relates to this user control is to be deleted.
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.