This is a migrated thread and some comments may be shown as answers.

Create an image template column from server side

4 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Covertix
Top achievements
Rank 1
Covertix asked on 17 Jun 2012, 08:43 AM
Hi,

I want to create a template column with an image, like in this example:

<telerik:GridTemplateColumn

HeaderText="test" HeaderStyle-Width="50px" ItemStyle-Width="16px"

UniqueName="test" AllowFiltering="false">

<ItemTemplate>

<asp:Image ID="StatusImage" runat="server" Width="16px" Height="16px"/>

</ItemTemplate>

but I want to create it on server side.

How can I do that?

Thanks.

</telerik:GridTemplateColumn>

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Jun 2012, 04:37 AM
Hello,

You can add template columns dynamically as shown below.
C#:
private class MyTemplate : ITemplate
{
    protected ImageButton img;
}
public void InstantiateIn(System.Web.UI.Control container)
{
 img = new ImageButton();
 img.ID = "ImageButton1";
 img.ImageUrl = "~/Images/img.gif";
 container.Controls.Add(img);
}
protected void Page_Init(object sender, EventArgs e)
{
   string templateColumnName = "TemplateColumn";
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
    templateColumn.HeaderText = templateColumnName;
        . . .
  Radgrid1.MasterTableView.Columns.Add(templateColumn);
}
Also check the following help documentation which explains the same.
Programmatic Creation

Thanks,
Shinu.
0
Covertix
Top achievements
Rank 1
answered on 18 Jun 2012, 08:32 AM
can I change tje image src based on the column data value?
0
Accepted
Shinu
Top achievements
Rank 2
answered on 19 Jun 2012, 05:03 AM
Hello,

Try the following code to achieve your scenario.
C#:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem item = (GridDataItem)e.Item;
    ImageButton img = (ImageButton)item.FindControl("ImageButton1");
    if (item["UniqueName"].Text == "text")
     {
        img.ImageUrl = "~/Images/img1.jpg";
     }
  }
}

Thanks,
Shinu.
0
Covertix
Top achievements
Rank 1
answered on 19 Jun 2012, 08:00 AM
Thank you very much for your help!
Tags
Grid
Asked by
Covertix
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Covertix
Top achievements
Rank 1
Share this question
or