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 07, 2003 11:03 AM by Jon Galloway
Filed under:

Comments

# re: Repeater ItemCommand Won't Fire without ViewState

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.

Hope this helps,
Clint

Saturday, October 11, 2003 5:39 PM by Clint Simon

# 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 09, 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

Yup it appears that in version 2.0 this started happening.   I followed the previous recommendation and I set the repeater viewstate to false and that did the trick.

Guess you can also follow the other suggested alternative and do rebinding on the page_load.

Just grateful for the feedback, I struggled 10 hours until I came across this site.  

Thank you

Friday, February 23, 2007 2:02 AM by DotNetFranco

# 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 03, 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 07, 2007 2:30 PM by Mike

# re: Repeater ItemCommand Won't Fire without ViewState

I am having this problem with a dnn module. I have a link button embeded into the repeater with the following code.

ID="lnkRecID" runat="server" CommandName="Update" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Item") %>'>Update

with the asp tags respectively at the beginning and end. No matter how I change the viewstate the link button will not initiate a postback.

Any suggestions?

Thursday, June 07, 2007 1:43 PM by James

# re: Repeater ItemCommand Won't Fire without ViewState

James I have been pulling my hair out with the same situation, where the viewstate issue does not apply.

In your datalist control, try setting CausesValidation="false".  It worked for me.

On a hunch, I remember having scripting issues where validation controls were on a page, but a control without any custom validation would not work. By setting CausesValidation="false" the control would work.

Well, there is no validation on this page, but I tried it, and that seems to have worked.

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

# 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

I am trying to get the values out of dropdowns that I added dynamically within a gridview control.  I have figured out how to make the selected values survive postbacks, but I haven't figured out how to get a reference to the dynamically added controls to read the values out of them. Any suggestions?

Friday, January 18, 2008 9:08 AM by Vinyl Shutters

# 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

Saved the day!   I had been googling "Repeater Header ItemCommand" and "Repeater Header Event Handlers" and this page was 3 or 4 pages deep in the results.  

Hopefully this comment will help to bump it up a little.

Tuesday, June 03, 2008 10:27 AM by talleman@lumpsum.com

# 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 06, 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 05, 2010 5:43 PM by Vasko

# re: Repeater ItemCommand Won't Fire without ViewState

Stupid me, setting CausesValidation="false" made my day after 4 hours struggling... Thanks guys for sharing this tip

Wednesday, April 14, 2010 12:58 PM by locusta74

# 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 04, 2011 8:29 PM by Praveen

Leave a Comment

(required) 
(required) 
(optional)
(required)