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

22 Comments

  • 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

  • I have the same problem suing a datagrid

  • I just included my bind within the

    if (!IsPostBack) statement and that fixed it.

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

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

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

  • 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

  • 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

  • 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!

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

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

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

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

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

  • 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

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

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

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

    Anyone knows why

  • You saved my day... Thanks

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

  • Saved my life too :) EVS off, rebind on

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

Comments have been disabled for this content.