Search code examples
c#asp.netviewstate

How to ensure viewstate is disabled on a large number of checkbox controls?


I have an ASP.NET web form that has several thousand checkbox controls (in a parent/child hierarchy). The page takes about 4 seconds to load. I'm trying to increase the performance so that the page only takes ~1-2 seconds to load. After doing some digging, it looks like the checkbox controls are generating a massive viewstate object (almost 1MB in the HTML file). If I use raw HTML checkboxes, the page renders very quickly (and I think it's due to the fact that the viewstate isn't being created). I tried going down the raw HTML chechbox route, but am having trouble calling ASP.NET methods from JS using dopostback. Anyway, my main goal is to eliminate the viewstate on the checkboxes. I've tried doing this by disabling viewstate on the checkbox controls as well as on the ASPX page. Regardless of what combo I use, the viewstate object is still being created (and as I said, it's massive). I've even tried to clear the viewstate object in the code behind at all the major events that fire. Still no luck at clearing out the viewstate. Any suggestions on what I can do to disable viewstate on the checkboxes to avoid the overhead of what looks like is causing a performance bottleneck?


Solution

  • One way to keep the page as it is, is to warp your check boxes, with a place holder and set EnableViewState="false" for this one.

    <asp:PlaceHolder runat="server" EnableViewState="false">
        <asp:CheckBox runat="server" ID="cbOne" />
    </asp:PlaceHolder>
    

    Other way is to place them all inside a custom control and set EnableViewState="false" for this control.