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

[Solved] GridCheckBoxColumn -> Images ?

6 Answers 209 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pbisiac
Top achievements
Rank 1
pbisiac asked on 23 May 2008, 04:59 PM
Hello, My Grid shows some columns bound to a datatable (one row per "customer"). At the end I put an unbound GridCheckBoxColumn and I wish to show two images (Online/Offline) programmatically when my customers go online or offline and I change the Checked property of my cell (perhaps is better to use some other type of control ?). Is it possible  ?
bye
Paolo

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 May 2008, 04:45 AM
Hi,

You can use a GridTemplateColumn with an Image in its ItemTemplate to display Images in RadGrid as shown below.

ASPX:
  <telerik:GridTemplateColumn UniqueName="TempCol" >  
    <ItemTemplate>        
        <asp:Image ID="Image1" runat="server"  ImageUrl="~/Image1.jpg"/> 
      </ItemTemplate>  
 </telerik:GridTemplateColumn>  

Thanks
Shinu.
0
pbisiac
Top achievements
Rank 1
answered on 26 May 2008, 02:01 PM
Hello, I tried and it works, thank you. But not still done.
I modified my .aspx file like that:

<Columns>
...................
</radG:GridButtonColumn>                   
<radG:GridTemplateColumn  UniqueName="OnLineImage">  
    <ItemTemplate>        
    <asp:Image ID="Image1" runat="server"  ImageUrl="bulb.jpg" HeaderText="On Line"/> 
      </ItemTemplate>  
</radG:GridTemplateColumn >
</Columns>

Now I wish to change this image programmatically when my website detects a user is "online", possibly refreshing just the image and not the whole page. but I cannot address the single image from a C# method. I tried to use buttons with images instead of templates and I could change the image, unfortunately this code:
ImageButton btn = (ImageButton)current["OnLineImage"].Controls[0];
ibtn.ImageUrl = "bulb_on.jpg";
refreshes all the page.

As you can see, i am posing two questions ;-) how to address a template field and how to trigger an ajax callback from server side. Sorry for my inexperience....
Best regards,

Paul
0
Princy
Top achievements
Rank 2
answered on 27 May 2008, 06:53 AM
Hi,

Try accessing Image in the ItemTemplate of a Grid using its StringID as shown below.

CS:
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            Image img = (Image)item["OnLineImage"].FindControl("Image1");  
        }  
    } 

Thanks
Princy.
0
pbisiac
Top achievements
Rank 1
answered on 27 May 2008, 08:07 AM
It works, thank you very much !

The last challenge...now the whole page get refreshed when I change
img.ImageUrl = "bulb_on.jpg";
or
img.ImageUrl = "bulb_off.jpg";
is it possible to Ajax it and to trigger the image change from server ? I could perhaps put a javascript timer in the web page who asks for changes in server status every, say, 10 seconds. But it sounds like a bad workaround....
Thank you again for your help,

Paolo
0
Shinu
Top achievements
Rank 2
answered on 27 May 2008, 08:22 AM
Hi,

Try setting the EnableAjax property of the Grid to true or use RadAjaxManger to ajaxify the Grid .

Postback vs. AJAX
AJAX Manager

Shinu
0
pbisiac
Top achievements
Rank 1
answered on 28 May 2008, 04:47 AM
Hello,

this didn't work. I mean, RadGrid1.Rebind() gets fired but nothing happens.
I tried also to programmatically build all my grid, same again, RadGrid1.Rebind() gets fired but nothing happens.

At the end, I decided to
- include a timer in javascript
<script language="javascript" type="text/javascript">
function setupTimer ()
{
    alert ("setupTimer !");
    setTimeout("fireTimer()",2000);
}
function fireTimer ()
{
    window["<%= RadGrid1.ClientID %>"].AjaxRequest( "<%= RadGrid1.UniqueID%>", "Rebind");
    //alert ("rebind !");
    setTimeout("fireTimer()",2000);
}
</script>

then to acivate the timer with <body onload="setupTimer()">

I used a TemplateColumn:
<radG:GridTemplateColumn UniqueName="On Line" HeaderText="On Line">
  <ItemTemplate>
    <asp:Image ID="OnLineImage" runat="server" ImageUrl='<%#     GetOnLineImage((bool)DataBinder.Eval(Container.DataItem, "On Line"))  %>'
    HeaderText="On Line" />
  </ItemTemplate>
 </radG:GridTemplateColumn>

and finally to write asp function in C#

    protected string GetOnLineImage(bool val)
    {
        if (val == true)
            return "bulb_on.jpg";
        else
            return "bulb.jpg";
    }

now RadGrid1.Rebind() works

Thank you for your help !
Paolo
Tags
Grid
Asked by
pbisiac
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
pbisiac
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or