Repeater ItemCommand Won't Fire without ViewState - Jon Galloway

Repeater ItemCommand Won't Fire without ViewState

There's something fishy going on between Repeater ItemCommands and ViewState. I had some asp:LinkButton controls in a HeaderTemplate that just wouldn't fire... until I enabled ViewState for the Repeater control. Once I knew that it had something to do with ViewState, I was able to see that others have run into this.

Odder still, others had experienced the opposite - ItemCommand only fires if ViewState is disabled.

It's easy to Google this up once you know to search on Repeater ItemCommand ViewState, but if you don't know that ViewState's the issue (and why would you?) it's a head scratcher.

Update: Clint Simon set me straight on this one:

“This is because on a postback the linkbutton that is inside your repeater doesn't exist unless you rebind the repeater. If viewstate is off the server can't fire the linkbutton.click event because the linkbutton doesn't exist in the control tree.

You can safely set enableviewstate to false on the repeater if you make sure to rebind the reapeater on each request weather there is a postback or not.”

Published Tuesday, October 7, 2003 11:03 AM by Jon Galloway
Filed under:

Comments

# re: VERY USEFUL .. THANKS

Seems like it depends on both EnableViewState (EVS) and Postback/rebind

This is my scenario ..

1. EVS=True, Rebind on postback: DID NOT WORK
2. EVS=True, No rebind: WORKED
3. EVS=False, Rebind: WORKED
4. EVS=False, No Rebind: DID NOT WORK, obviously

Anyways, helped me to get it working ! Thanx

Thursday, April 15, 2004 6:58 PM by Naveen

# re: Repeater ItemCommand Won't Fire without ViewState

I have the same problem suing a datagrid

Thursday, June 10, 2004 4:08 PM by kaw

# re: Repeater ItemCommand Won't Fire without ViewState

I just included my bind within the
if (!IsPostBack) statement and that fixed it.

Thursday, June 10, 2004 4:21 PM by kaw

# re: Repeater ItemCommand Won't Fire without ViewState

I have used linkbutton inside repeater control but when i click on the linkbutton repeater's item command is not firing.

please provide solution.

Monday, July 24, 2006 8:50 AM by Ganesh Ahire

# re: Repeater ItemCommand Won't Fire without ViewState

Thanks Jon Galloway .

I have almost Spend whole day on this technical issue.but at last i found idea from this article and my problem was solved.

Wednesday, August 9, 2006 1:44 AM by Fai qshah

# re: Repeater ItemCommand Won't Fire without ViewState

I had the same problem, but playing with the viewstate property didn't help the problem. Until I realised the content placeholder's viewstate was disabled and so it didn't matter how I changed my viewstate of the repeater. After viewstate enabled of the placeholder it worked perfectly.

Monday, November 27, 2006 11:45 AM by Hans Nuttin

# re: Repeater ItemCommand Won't Fire without ViewState

i am trying to add button controls at run time to a placeholder in datalist's footer template, but ItemCommand is not firing for these button controls. need help

Tuesday, December 26, 2006 6:15 AM by Ajit

# re: Repeater ItemCommand Won't Fire without ViewState

I have some buttons within a .ascx control and a repeater on the page. If i don't bind on load (no matter if it is on postback or not), then the linkbuttons on the repeater do not fire the repeater's itemcommand. if i do bind the repeater on load every time then the buttons that are not in the repeater don't fire. i have tried viewstate combinations with the binding, but it comes down to the fact that what works for the repeater breaks the buttons outside the repeater and vice versa.

pseudo code looks like this:

on load()

   BindRepeater()

end

linkbutton_onclick()

   dynamically create a criterion for the   repeater's dataset

   BindRepeater()

End

Repeater_itemcommand()

   set some session variables

   BindRepeater()

end

BindRepeater()

bind repeater to a dynamic sql statement

end

Sunday, December 31, 2006 2:45 AM by manny

# re: Repeater ItemCommand Won't Fire without ViewState

The viewstate solution didn't work for me, so I added in an event and delegate to the template and tried to use that to bubble up the click event, but it didn't work. It seems that the ItemCommand isn't firing because the linkbutton event isn't firing. For some reason the linkbutton becomes an ordinary html button and just posts back - no event.

That's as far as I can be bothered to take it, I'm moving back to GET since POST isn't working. It'll save me hours of mucking around. With every release there's something wrong with the f***in' repeater - MS sort it out!

Saturday, March 3, 2007 10:58 PM by iainspeed

# re: Repeater ItemCommand Won't Fire without ViewState

Well I had a similar problem with datalist control, while adding buttons to footer control dynamically. But in my case when I made the EVS=true for datalist control, it worked.

Is this a bug or its the way to do it??????????????

Thank you.

Friday, March 16, 2007 10:55 AM by Mehul

# re: Repeater ItemCommand Won't Fire without ViewState

I had the same problem but I had turned off viewstate for the contentPlaceholders on my master page.

Monday, May 7, 2007 2:30 PM by Mike

# re: Repeater ItemCommand Won't Fire without ViewState

I'm sorry. Place CausesValidation="false" in the markup of your linkbutton.

Wednesday, August 22, 2007 12:09 PM by John

# re: Repeater ItemCommand Won't Fire without ViewState

If you bind in your Page_PreRender() event to react to user inputs like I do, then disabling viewstate on your repeater *will* cause your linkbuttons inside the repeater to no fire their clicked event (because they haven't been created yet).  So, the simplest solution I found was to just bind to an empty ArrayList in the Page_Load() event, and not touch any other code.

protected void Page_Load(object sender, EventArgs e)

   {

       //If we don't rebind the repeater here, and viewstate is false, clicking linkbuttons won't raise events.

       Repeater1.DataSource = new ArrayList();

       Repeater1.DataBind();

   }

Works great and still seems relatively lightweight..  Feel free to expand on this..

Saturday, January 19, 2008 5:05 PM by Steve

# re: Repeater ItemCommand Won't Fire without ViewState

For controls added at run time (dynamically) in repeater(or any where else) setting id property helps proper handling of Item command event. (make sure that dynamically added controls are loaded evry time before viewstate is set.

Saturday, September 20, 2008 12:31 PM by Sudheer Akuthota

# re: Repeater ItemCommand Won't Fire without ViewState

John,

Thanks!

Setting CausesValidation="false" on the LinkButton fixed the issue I was having with my LinkButton event not firing inside a repeater. Works great now!

Mike

Monday, September 22, 2008 4:29 PM by Mike

# re: Repeater ItemCommand Won't Fire without ViewState

I have the same issue with "DataGrid" control. Though I bind it in the page_load, still the problem persists. When i select the button, the grid vanishes.

Thursday, September 25, 2008 7:43 AM by mdmanisha

# re: Repeater ItemCommand Won't Fire without ViewState

Thanx a million ....

Both ...

Setting EVS=False

     &

Setting EVS=True and including [b]if(!ispostback)[/b] on page_load event of my webform worked....

Thanx once again...

Regards,

Rajan Arora....

Saturday, February 28, 2009 5:01 AM by Mr.Rajan

# re: Repeater ItemCommand Won't Fire without ViewState

For me, the EVS doesn't matter. It works ONLY when I rebind all the time

Anyone knows why

Wednesday, March 25, 2009 1:07 PM by indyanguy

# re: Repeater ItemCommand Won't Fire without ViewState

You saved my day... Thanks

Tuesday, May 26, 2009 6:56 PM by Jeff

# re: Repeater ItemCommand Won't Fire without ViewState

how i use repeters items id  on page.

Friday, November 6, 2009 1:19 AM by Rakesh Meel

# re: Repeater ItemCommand Won't Fire without ViewState

Yeah, just putting the causeValidation to false in the linkbutton fix it for me. Tnx!

Sunday, December 13, 2009 5:58 PM by Raymond

# re: Repeater ItemCommand Won't Fire without ViewState

Saved my life too :) EVS off, rebind on

Friday, March 5, 2010 5:43 PM by Vasko

# re: Repeater ItemCommand Won't Fire without ViewState

I had the same problem, But i had a textbox which was mandatory and invisible. I changed the CausesValidation=False for the Button and it worked. Stupid Me:-)

Friday, March 4, 2011 8:29 PM by Praveen