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?

4
  • Show us the complete code of your page (markup & backend) if possible. Commented Aug 24, 2009 at 12:17
  • Do you have viewstate turned on for this page? Commented Aug 24, 2009 at 12:21
  • 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 Commented Aug 24, 2009 at 14:09
  • 2
    Possible duplicate of ASP.Net: why is my button's click/command events not binding/firing in a repeater? Commented May 31, 2018 at 14:55

12 Answers 12

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

Sign up to request clarification or add additional context in comments.

6 Comments

+1, in past I had same problem with viewstate. (I would like to know your workround)
+1 this makes a lot more sense. maybe add this to your answer. msdn.microsoft.com/en-us/library/ms972976.aspx
No, ViewState is enabled (I have not disabled it anywhere in the application anyway)
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
+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.
|
6

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

Comments

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.

Comments

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 :)

Comments

2

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

Comments

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

Comments

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 ...

Comments

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.

1 Comment

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
0

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

1 Comment

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: msdn.microsoft.com/en-us/library/ms178472.aspx
0

Try this:

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

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.