472,955 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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:LinkButton ID="lnkItem" Runat="server"
EnableViewState="true" CommandName="DrillDown"></asp:LinkButton>
<br>
</ItemTemplate>
</asp:Repeater>

The OnItemCommand event for the Repeater is connected to the
rptCtrl1_OnItemCommand function:

public void rptCtrl1_OnItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
LinkButton lnkButton = (LinkButton)e.CommandSource;
string param = lnkButton.CommandArgument.ToString();
makeDataBind(param);
}
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 5301
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:LinkButton 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_lnkItem without the
repeater recreating the control, or you binding the data again on each
postback.

HTH,

bill
<ol**********@yahoox.com> wrote in message
news:lq********************************@4ax.com...
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:LinkButton ID="lnkItem" Runat="server"
EnableViewState="true" CommandName="DrillDown"></asp:LinkButton>
<br>
</ItemTemplate>
</asp:Repeater>

The OnItemCommand event for the Repeater is connected to the
rptCtrl1_OnItemCommand function:

public void rptCtrl1_OnItemCommand(object source,
System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
LinkButton lnkButton = (LinkButton)e.CommandSource;
string param = lnkButton.CommandArgument.ToString();
makeDataBind(param);
}
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
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...
0
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
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">...
2
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
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...
1
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...
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...
1
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...
0
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.