views:

2993

answers:

5

OK, I've used repeaters literally hundreds of times without problems but something has gone awry today. I have a repeater and I'm subscribing to the itemCommand event, but when my command runs, the page posts back but the event isn't fired.

To get around this I'm having to do my databinding on each postback.

My repeater looks like this:

<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
    <li>
    <asp:Label id="Label" runat="server" />
    <asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
    </li>
</ItemTemplate>
</asp:Repeater>

and my codebehind like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    SetupPage();
    }
}

private void SetupPage()
{
    // Do other stuff

    MyRepeater.DataSource = Repository.GetStuff()
    MyRepeater.DataBind();
}


protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{


// Do all my stuff here
}

MyRepeater_ItemCommand is not getting called unless I comment out the if (!IsPostBack) line. Once that is commented out and the repeater is getting databound on each postback it works OK. I've done this in so many other pages but on this on it just doesn't seem to work.

Anyone else come across this behaviour or have a solution?

A: 

Do you have viewstate turned on for this page?

Tim Meers
Yup, ViewState is turned on
Ciaran
+3  A: 

Most likely, you have disabled ViewState for the page.

The reason is that when you execute the postback, all the controls in the repeater are rebuild from the data in the viewstate normally. Then the object that should receive the event is identified based on the ID of the control, and the event is routed.

If you disable the viewstate, the control tree is not rebuild during postback, and therefore the control that should receive the event does not exist in memory. So the event dies.

If you really want to disable the viewstate, but still want to receive the event, I have a workaround (and it's not dirty at all). I've long been thinking about writing a blog entry about it, so if you want, I can take a bit time off my normal chores, and describe it.

Pete
+1, in past I had same problem with viewstate. (I would like to know your workround)
Cleiton
+1 this makes a lot more sense. maybe add this to your answer. http://msdn.microsoft.com/en-us/library/ms972976.aspx
Bdiem
No, ViewState is enabled (I have not disabled it anywhere in the application anyway)
Ciaran
Oh, and other items on the page that are databound keep their state when I'm not binding on each postback, so I really don't think it's a ViewState issue
Ciaran
+1 great discription. It also might be nice to see that work around. I use the Item Command on a heavy page and it make it that much more having to lug the viewstate.
Tim Meers
@Ciaran - It does sound strange, because you say that it works if you rebind the repeater during postback. That does really smell like the repeater is not getting deserialized for some reason. Can it be that there is other code somewhere that accidentally clears the repeater.To the other interested parties, I described my no-viestate repeater event workaround here. http://petesdotnet.blogspot.com/2009/08/asp.html
Pete
A: 

I am not positive about this, but you might have to set the CommandName and optionally CommandArgument properties for the button causing the ItemCommand event. Otherwise ASP.NET would assume that there is no button on the page, which you'd like to fire the event. You can try that.

Plus, if you're not differentiating between command names, why not use each button's Click event instead? Just subscribe to those in the repeater's ItemCreated or ItemDataBound.

Slavo
As you can see from the code above I have specified a CommandName and a CommandArgument.I already have a work around for this by databinding on each postback - but I don't want to have to do that
Ciaran
A: 

Try using Page_init instead of Page_load and that should fix the problem.

Chuck Jones
Not really. In the Init event, "If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state." See: http://msdn.microsoft.com/en-us/library/ms178472.aspx
Venemo
A: 

Here is the solution on Stack Overflow: link. Hope this helps!

EDIT: I noticed that your Repeater subscribes for the ItemCommand event from markup. If I do it that way, it is not firing for me, either!

I must also point out that the event only works AFTER the repeater has been data bound. So, try subscribing for the event in the code, and after calling the DataBind() method.

Venemo
As you can see from my original code above, I was already using the ItemCommand event with a CommandArgument and a CommandName
Ciaran
I updated my answer to clarify. :)
Venemo