20

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?

Ciaran O'Neill
  • 2,572
  • 6
  • 34
  • 40
  • Show us the complete code of your page (markup & backend) if possible. – Bdiem Aug 24 '09 at 12:17
  • Complete markup won't fit in the comments box, but there isn't anything unusual in the page at all. Using master pages (no reference to disabling ViewState in master pages either) and Content Placeholders and then a repeater - very simple. Code behind has some more database/repository access code but again nothing unusual – Ciaran O'Neill Aug 24 '09 at 14:09
  • Do you have viewstate turned on for this page? – Tim Meers Aug 24 '09 at 12:21
  • 2
    Possible duplicate of [ASP.Net: why is my button's click/command events not binding/firing in a repeater?](https://stackoverflow.com/questions/42396/asp-net-why-is-my-buttons-click-command-events-not-binding-firing-in-a-repeate) – AStopher May 31 '18 at 14:55

12 Answers12

24

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.

Edit: The workaround is described here: http://petesdotnet.blogspot.dk/2009/08/asp.html

Pete
  • 12,206
  • 8
  • 54
  • 70
  • +1, in past I had same problem with viewstate. (I would like to know your workround) – Cleiton Aug 24 '09 at 13:15
  • +1 this makes a lot more sense. maybe add this to your answer. http://msdn.microsoft.com/en-us/library/ms972976.aspx – Bdiem Aug 24 '09 at 13:31
  • No, ViewState is enabled (I have not disabled it anywhere in the application anyway) – Ciaran O'Neill Aug 24 '09 at 13:59
  • 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 O'Neill Aug 24 '09 at 14:05
  • +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 Aug 24 '09 at 18:26
  • @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 Aug 26 '09 at 09:11
6

Remove if (!IsPostBack) as this is preventing the repeater from rebinding, and the item command event could not find the row after postback.

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Prashant
  • 86
  • 1
  • 2
5

I have the same problem and aside from using update panel, I have a required field validator in my modal. I found out that the LinkButtons in my repeater triggers the requiredFieldValidor event and then I added CausesValidation="false" in the LinkButtons of my repeater. Works as expected.

Francis Saul
  • 728
  • 2
  • 16
  • 37
2

I removed PostBackUrl property in linkbutton and ItemCommand fired. I think postback runs first.

2

I have this problem in a repeater when I use ImageButton ... I have search the net for this solution when LinkButton work, but not ImageButton ...

Then I think, LinkButton work? so i will use it :)

<asp:LinkButton  CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>

</asp:LinkButton> 

So, the image is inside the <A> tag

have fun :)

McDowell
  • 107,573
  • 31
  • 204
  • 267
WerdDomain
  • 21
  • 1
1

That may be you have set Validations on your page. So set an new attribute, causevaliation = "false" to Link button. M sure it will solve the problem

meekash55
  • 307
  • 3
  • 6
1

I had a similar issue - turned out some discreet validation controls were firing elsewhere on the page. It only took me a day to figure it out ...

immutabl
  • 6,857
  • 13
  • 45
  • 76
0

Try this:

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

If you use nested-repeater, you should rebind your inner repe

Sawyer
  • 568
  • 5
  • 17
0

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
  • 15,255
  • 11
  • 47
  • 60
  • 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 O'Neill Aug 24 '09 at 14:00
0

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

  • 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 Apr 28 '10 at 11:56
0

Here Is the code you have to use in code behind..

after PageLoad event,

 protected void Page_Load(object sender, EventArgs e)
 {

 }


 protected void Page_LoadComplete(object sender, EventArgs e)
 {
      // Bind Your Repeater here
      rptUser();
 }

now you can fire your Itemcommand..if you get Output please mark answer as right thanks

Pragnesh
  • 77
  • 2
0

One other thing that it could be (as it just happened to me): if your databind takes place when your page is prerendered, it won't process the item command. Switch it to load or init and you'll be fine.

SEFL
  • 539
  • 4
  • 15