Add to TemplateField an ImageButton at run time

0

I have this

TemplateField imgTemp = new TemplateField();
    imgTemp.ItemTemplate = new ColumnaTemplate("Seleccion");
    imgTemp.EditItemTemplate = new ElementoTemplate("Seleccion");
    imgTemp.HeaderText = "Seleccion";
    GridView1.Columns.Add(imgTemp);

But I still have to complete my code so that it remains the same as what this does

 <asp:TemplateField HeaderText="">
                           <ItemTemplate >
                               <asp:ImageButton runat="server" CommandName="Select" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  Width="70px" 
                                Height="60px" ToolTip="Seleccion" 
                                ImageUrl = "~/View/Imagen/Perfil.JPG" />
                           </ItemTemplate>
                        </asp:TemplateField> 
    
asked by Efrain Mejias C 05.07.2016 в 02:07
source

1 answer

2

You must create a class that inherits ITemplate in order to create the template column

In the article I explain on the subject

[GridView] ITemplate - Columns defined in runtime

You must define a class that inherits from ITemplate, then you have to assign this to the properties of TemplateField and add it to the Columns collection

TemplateField tempDesc = new TemplateField();
tempDesc.HeaderTemplate = new GridViewHeaderTemplate("Descripcion Producto");
tempDesc.ItemTemplate = new GridViewItemTemplate("Descripcion");
tempDesc.EditItemTemplate = new GridViewEditTemplate("Descripcion");
GridView1.Columns.Add(tempDesc);
    
answered by 05.07.2016 / 02:56
source