Search code examples
c#asp.netgridviewtemplatefield

Auto-generate template fields OR gridviews?


This question is related to Changing hardcoded dates to dynamic.

Long story short, I have a gridview with a bunch of templatefields for dates that are hardcoded in. I need these templatefields to change based on a boolean in the code behind. Is it possible to auto-generate these based on what the bool is equal to?

Would making two sets of gridviews be a viable option and have only have one visible at a time based on the bool?

I did some code-behind stuff to make the correct dates in the templatefields, but whenever there is a callback (which I can't modify or get rid of), the gridview and templatefields revert back to the ones hardcoded in the .aspx file.


Solution

  • Why don't just

    <TemplateField>
      <% if ( boolFromCodebehind ) { %>
      Hardcoded date 1
      <% } else { %>
      or even <%= codebehindVariable %>
      <% } %>
    <TemplateField>
    

    Such conditional markup works outside of templates but I hope it works there too.