Put an ImageButton in a TemplateColumn manually

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nath Rossel

    Put an ImageButton in a TemplateColumn manually

    Hello,

    I have to create a DataGrid with dynamic columns and I want to put an ImageButton in a TemplateColumn. If the DataGrid is NOT DYNAMIC, I put this code on the .aspx page like this : (this first part of code is just to explain, it isn't what I want to do)

    Code:
    <asp:TemplateColumn>
        <ItemTemplate>
            <asp:ImageButton ID="ibSelect" runat="server" CausesValidation="False" 
                CommandName="Select" ImageUrl="~/Templates/Images/icon_select.gif" />
        </ItemTemplate>
        <HeaderStyle Width="16px" />
    </asp:TemplateColumn>
    Now, I want to do it MANUALLY on the page aspx.cs. I have to declare the same type of objects and set the same values, like this :

    It doesn't work because I DON'T FIND HOW TO PUT MY IMAGEGUTTON IN THE TEMPLATECOLUMN.


    Code:
    TemplateColumn selectTemplate = new TemplateColumn();
    
                
    ImageButton selectImageButton = new ImageButton();
    selectImageButton.ID = "ibSelect";
    selectImageButton.CausesValidation = false;
    selectImageButton.CommandName = "Select";
    selectImageButton.ImageUrl = "~/Templates/Images/icon_select.gif";
    
    
    Control cselectImageButton = new Control();            
    cselectImageButton.Controls.Add(selectImageButton);
    cselectImageButton.ID = "sel";
    
    selectTemplate.ItemTemplate = (ITemplate) cselectImageButton.Controls[0];
    selectTemplate.HeaderStyle.Width = 16;
    
    
    dg.Columns.Add(selectTemplate);
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I think you will be interested in reading about the ITemplate Interface. You can create a class that implements this interface to use as a TemplateItem so that you can dynamically add them to your GridView. Your class will add the LinkButton to the correct place (depending on whether the item is being added in the body, header, or footer).

    -Frinny

    Comment

    Working...