ViewState and Repeater Control

O

olduncleamos

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
 
W

William F. Robertson, Jr.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top