Setting ImageURL in GridButtonColumn

3 posts, 0 answers
  1. Rich
    Rich avatar
    25 posts
    Member since:
    Jun 2010

    Posted 28 Jun 2010 Link to this post

    I have a GridButtonColumn defined in within a Grid control which needs to display diffeent images depending on the data source row. 

    However, there doesn't seem to be a way of setting the ImageURL property of this control from a data source column value.  How can this be done?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Jun 2010 Link to this post

    Hello Richard,

    You can change the ImageUrl of the Buttons in ItemDataBound event after checking  for the condition. Here is an example.

    Markup:
     
      
     <MasterTableView AutoGenerateColumns="False" EditMode="InPlace" CommandItemDisplay="Top"  
                    DataSourceID="SqlDataSource1" DataKeyNames="CustomerID">  
               <Columns>  
                    <telerik:GridButtonColumn ButtonType="ImageButton" UniqueName="ButtonColumn">  
                    </telerik:GridButtonColumn>  
                    . . .  

    Code behind:
      
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                GridDataItem item = (GridDataItem)e.Item;  
                ImageButton imgButton = (ImageButton)item["ButtonColumn"].Controls[0];  
                if (item.GetDataKeyValue("CustomerID").ToString() == "ANATR"// I set the DataKeyNames as CustomerID  
                {  
                    imgButton.ImageUrl = "../Images/Img1.gif";  
                }  
                else  
                {  
                    imgButton.ImageUrl = "../Images/Img2.gif";  
                }  
            }               
        }  


    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rich
    Rich avatar
    25 posts
    Member since:
    Jun 2010

    Posted 29 Jun 2010 Link to this post

    Thank you - this should work
Back to Top