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

Radgrid conditional image template column

5 Answers 858 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arnie Vargas
Top achievements
Rank 1
Arnie Vargas asked on 13 Oct 2011, 05:54 PM
Please help. I have one column in each row of my radgrid  in which i want to show an image. I am  getting values from database reading 1 or 2 for that particular column and for each of these values i want to show different images for different values. For example if i am getting 1 from database I want to show a particular image and for 2 i want to show a different image. Right now I am hardcodeing the red_circle image as I have not incorporated the database logic. So as of now when I click the imagebutton it is working as intended(it changes the image from red to green or green to red) I just need to figure out how to dynamically tell it which image to load depending on database column value of 1 or 2.
Thanks!

--aspx code

<

Columns>

 <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID" Visible="False">

 </telerik:GridBoundColumn>

 <telerik:GridBoundColumn DataField="NAME" HeaderText="NAME" SortExpression="NAME" UniqueName="NAME">

 </telerik:GridBoundColumn>

 <telerik:GridBoundColumn DataField="TITLE" HeaderText="TITLE" SortExpression="TITLE" UniqueName="TITLE">

 </telerik:GridBoundColumn>

 <telerik:GridTemplateColumn HeaderText="STATUS" UniqueName="STATUS">

 <ItemTemplate>

 <asp:ImageButton ID="lnkStatus" runat="server" ImageUrl = "\Images\circle_red.png" CommandName="StatusChange" CommandArgument='<%# Eval("ID") %>'></asp:ImageButton>

 </ItemTemplate>

 </telerik:GridTemplateColumn>

 </Columns>

 
--.vb code

 Protected Sub gvStaff_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles gvStaff.ItemCommand

 

If e.CommandName = "StatusChange" Then

If TypeOf e.Item Is GridDataItem Then

  

Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)

Dim statusButton As ImageButton = DirectCast(item("STATUS").FindControl("lnkStatus"), ImageButton)

statusButton.ImageUrl = "\Images\circle_green.png"

 End If

 End If

 End Sub

 

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Oct 2011, 09:27 AM
Hello,

try with below code snippet.
ImageUrl='<%# Eval("ID") == null ? "~/images/1.gif" : "~/images/2.gif" %>'
 
ImageUrl='<%# Eval("ID").ToString() == "1" ? "~/images/1.gif" : "~/images/2.gif" %>'


Let me know if any concern.

Thanks,
Jayesh Goyani
0
usman
Top achievements
Rank 1
answered on 04 Feb 2012, 04:10 PM
for multiple Ifs?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Feb 2012, 04:44 PM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                ImageButton lnkStatus = item.FindControl("lnkStatus") as ImageButton;
                lnkStatus.ImageUrl = "set your image here";
            }
        }


Thanks,
Jayesh Goyani
0
usman
Top achievements
Rank 1
answered on 05 Feb 2012, 06:34 PM
please have a look 

http://www.telerik.com/community/forums/aspnet-ajax/grid/event-for-databinding-of-gridtemplate-column.aspx 

without if statement we get error of not using new operator for creating image button 
0
Antonio Stoilkov
Telerik team
answered on 08 Feb 2012, 05:19 PM
Hello Usman,

I have assembled a sample project demonstrating the desired functionality. In order for the exception to be resolved a check if the item is GridDataItem is needed as it is shown in the example:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        ImageButton up_url = item.FindControl("vote_up") as ImageButton;
        ImageButton down_url = item.FindControl("vote_down") as ImageButton;
 
        if (bool.Parse(item.GetDataKeyValue("liked").ToString()))
        {
            up_url.ImageUrl = "~/images/up_no";
            down_url.ImageUrl = "~/images/down_no.png";
        }
        else
        {
            up_url.ImageUrl = "~/images/up_logo.png";
            down_url.ImageUrl = "~/images/down_logo.png";
        }
    }
}

Additionally, you could go through the help article below for more in depth information on the topic of accessing controls in RadGrid.

Kind regards,
Antonio Stoilkov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Arnie Vargas
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
usman
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or