Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to make DataGrid TemplateColumn ItemTemplate and EditItemTemplate the same without duplicating code?

How to make DataGrid TemplateColumn ItemTemplate and EditItemTemplate the same without duplicating code?

Scheduled Pinned Locked Moved ASP.NET
csharphtmltutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyrilix
    wrote on last edited by
    #1

    Currently, I have something like this:

    asp:DataGrid
    asp:TemplateColumn
    <ItemTemplate>
    //some markup that tells us what controls to draw for ItemTemplate
    </ItemTemplate>
    <EditItemTemplate>
    //same markup as in ItemTemplate which should not be duplicated
    </EditItemTemplate>
    </asp:TemplateColumn>
    </asp:DataGrid>

    I was wondering if there was a way (either behind the scenes in C#) or in the markup itself, to make it so that we don't have to duplicate the markup. There was a thread I found for the FormView control, where we could set FormView.EditItemTemplate = FormView.ItemTemplate, but unfortunately, this is not available for the DataGrid TemplateColumn type.

    S 1 Reply Last reply
    0
    • C Cyrilix

      Currently, I have something like this:

      asp:DataGrid
      asp:TemplateColumn
      <ItemTemplate>
      //some markup that tells us what controls to draw for ItemTemplate
      </ItemTemplate>
      <EditItemTemplate>
      //same markup as in ItemTemplate which should not be duplicated
      </EditItemTemplate>
      </asp:TemplateColumn>
      </asp:DataGrid>

      I was wondering if there was a way (either behind the scenes in C#) or in the markup itself, to make it so that we don't have to duplicate the markup. There was a thread I found for the FormView control, where we could set FormView.EditItemTemplate = FormView.ItemTemplate, but unfortunately, this is not available for the DataGrid TemplateColumn type.

      S Offline
      S Offline
      Samer Aburabie
      wrote on last edited by
      #2

      Greetings, I think the thread you read already gave you the correct way ... its only a matter of casting the template column correctly then set the EditItemTemplate property to ItemTemplate property ... here is the code:

      TemplateField colmun = this.GridView1.Columns[0] as TemplateField;
      colmun.EditItemTemplate = colmun.ItemTemplate;

      Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

      C 1 Reply Last reply
      0
      • S Samer Aburabie

        Greetings, I think the thread you read already gave you the correct way ... its only a matter of casting the template column correctly then set the EditItemTemplate property to ItemTemplate property ... here is the code:

        TemplateField colmun = this.GridView1.Columns[0] as TemplateField;
        colmun.EditItemTemplate = colmun.ItemTemplate;

        Sincerely Samer Abu Rabie Note: Please remember to rate this post to help others whom reading it.

        C Offline
        C Offline
        Cyrilix
        wrote on last edited by
        #3

        Ah, I didn't think of that. Thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups