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

Conditional Display of ASP:IMAGE in GridTemplateColumn

4 Answers 489 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 29 Sep 2008, 03:09 PM
Hi,

How do I conditionally set the imageurl property of an ASP:IMAGE control in a GridTemplateColumn?

I can clear the controls to remove the image completely, but am unsure how to cast the Controls(0) collection to get an ASP:IMAGE object? I have tried casting to Image, but this obviously isnt an ASP:IMAGE type, but something from Graphics.


I can do conditional formatting like this, but want to do it programatically as I may want to plug in different images:

<

rad:GridTemplateColumn UniqueName="ButtonColumn">

<ItemTemplate>

<asp:Image runat="server" ImageUrl='<%# IIF(TypeOf Eval("SDC_A") Is System.DBNull, "~/Images/warning.gif","~/Images/tick.gif") %>' Width="10" Height="10"/>

</ItemTemplate>

</rad:GridTemplateColumn>



Protected

Sub grdStudyMilestones_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles grdStudyMilestones.ItemDataBound

'Is it a GridDataItem

If (TypeOf (e.Item) Is GridDataItem) Then

'Get the instance of the right type

Dim dataBoundItem As GridDataItem = e.Item

'Check the formatting condition

If IsDate(dataBoundItem("SDC_A").Text) Then

dataBoundItem(

"SDC").BackColor = Color.Gray

Else

' NEED TO ACCESS THE CONTROL SPECIFICALLY
dataBoundItem(

"ButtonColumn").Controls.Clear()

End If

End If

End Sub



4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Sep 2008, 03:22 AM
Hi Stephen,

Check out the following help article which explains how to achieve the desired scenario.
Conditional image display in GridButtonColumn/GridTemplateColumn

Regards
Shinu
0
Stephen
Top achievements
Rank 1
answered on 30 Sep 2008, 07:33 AM
Hi,

Thanks.  I have worked through this sample, but dont need a button, so was trying to do this with a GridTemplateColumn and an ASP Image.  I dont want to use a button as there is no action to be performed.

Any help appreciated.

Regards
i
0
Princy
Top achievements
Rank 2
answered on 30 Sep 2008, 11:25 AM
Hello Stephen,

    As I understand, you want to display different images for your TemplateColumn based on certain conditions.
aspx:
  <telerik:GridBoundColumn DataField="DataType" UniqueName="DataType" HeaderText="DataType">         
 </telerik:GridBoundColumn>  
 <telerik:GridTemplateColumn UniqueName="TemplateColumn1"
    <ItemTemplate> 
       <asp:Image ID="Image1" runat="server" />          
    </ItemTemplate> 
 </telerik:GridTemplateColumn> 

cs:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item;            
            Image img = (Image)item["TemplateColumn1"].FindControl("Image1"); 
            if (item["DataType"].Text == "String") 
            { 
                img.ImageUrl = "~/images/Delete.gif"
            } 
            else 
            { 
                img.ImageUrl = "~/images/New Bitmap Image.bmp"
            } 
        } 

Thanks
Princy.
0
Stephen
Top achievements
Rank 1
answered on 30 Sep 2008, 11:27 AM
Hi,

Thats exactly what i want to do thanks.  When I tried casting to Image, using the Controls(0) syntax I first tried, it seemed as though I was trying to cast to a class in the System.Graphics namespace...

Regards

i
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Stephen
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or