I have a RadGrid and I would like to have a default url image show in the grid when none is available. I have the code in the itemdatabound because before i am loading flash files as well as normal .jpg. I'm trying to figure out this code but I don't know if I am doing it wrong or if there is an easier way. My code is below.
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
String FileType = DataBinder.Eval(e.Item.DataItem, "FileTypeID").ToString();
TableCell TemplateCell = (e.Item as GridDataItem)["Image1"] as TableCell;
if (FileType == "swf")
{
TemplateCell.Text =
String.Format("<div id=\"flashcontent\"><embed type=\"application/x-shockwave-flash\" src=\"{0}\" id=\"sotester\" name=\"sotester\" quality=\"high\" wmode=\"transparent\" height=\"100\" width=\"290\" /></div>", DataBinder.Eval(e.Item.DataItem, "PhysicalPath"));
}
else if (FileType == null )
{
Image myImage = new Image();
myImage.ImageUrl =
"~/DataImages/missing.jpg";
}
else
{
Image myImage = new Image();
myImage.ImageUrl =
DataBinder.Eval(e.Item.DataItem, "PhysicalPath").ToString();
myImage.Width =
Unit.Pixel(16);
myImage.Height =
Unit.Pixel(16);
myImage.AlternateText =
"someimage";
TemplateCell.Controls.Add(myImage);
}
}
}
When it runs i can see the images and the flash but the empty images still don't have the missing jpg appearing
6 Answers, 1 is accepted
You have missed to add the image to the table cell, that's why it does not appear:
myImage.ImageUrl = "~/DataImages/missing.jpg";
TemplateCell.Controls.Add(myImage);
All the best,
Dimo
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Hello,
I added your line it still doesn't work.
this is my .aspx code for the same column
<telerik:GridTemplateColumn SortExpression="CompanyName"
UniqueName="TemplateColumn1"><HeaderTemplate>Images</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
CommandName='<%#Eval("ImageID") %>' Height="92"
ImageUrl='<%#(Eval("PhysicalPath")== DBNull.Value) ? "~/missing.jpg" : ("~/"+ Eval("PhysicalPath")) %>' OnClick="imageButton_Click" Width="92" />
</ItemTemplate>
</telerik:GridTemplateColumn>
I did add your line and its still blank. i even made a column in the databse that holds the value of the missing.jpg thinking maybe it was a typo some where on my part. My code with all of the things that I've tried commented out.
protected
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
String FileType = DataBinder.Eval(e.Item.DataItem, "FileTypeID").ToString();
TableCell TemplateCell = (e.Item as GridDataItem)["TemplateColumn1"] as TableCell;
if (FileType == "swf")
{
TemplateCell.Text =
String.Format("<div id=\"flashcontent\"><embed type=\"application/x-shockwave-flash\" src=\"{0}\" id=\"sotester\" name=\"sotester\" quality=\"high\" wmode=\"transparent\" height=\"100\" width=\"290\" /></div>", DataBinder.Eval(e.Item.DataItem, "PhysicalPath"));
}
else if (FileType == null)
{
Image defaultImage = new Image();
defaultImage.ImageUrl =
"~/DataImages/missing.jpg";
//defaultImage.ImageUrl = DataBinder.Eval(e.Item.DataItem, "LinkToUse").ToString();
TemplateCell.Controls.Add(defaultImage);
}
else
{
Image myImage = new Image();
myImage.ImageUrl =
DataBinder.Eval(e.Item.DataItem, "PhysicalPath").ToString();
//myImage.Width = Unit.Pixel(16);
//myImage.Height = Unit.Pixel(16);
//myImage.AlternateText = "someimage";
//TemplateCell.Controls.Add(myImage);
}
}
}
I'd really appreciate the help.
We have replied to your question in a similar support ticket of yours.
I checked your account and it occurs that you have opened several support tickets and forum threads for the same thing. It is highly recommended that you keep related questions in one support thread or a forum post, so that we can easily keep track of your support history and provide better answers in a shorter time.
Greetings,
Dimo
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Thanks,
Amanda
No, I never found the answer but we just left the link in its space on the grid and then when the user clicks they go to another page where we were able to hard code a missing image in the grid.
If i hear anything else I'll let you know.
Toni
This is what I have for showing the images, and if there is no image supplied, I have a blank .gif that shows instead.
protected void RadGrid1_ItemDataBound1(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
GridDataItem dataItem = e.Item as GridDataItem; |
Image rtypepic = (Image)e.Item.FindControl("rtypepic"); |
rtypepic.ImageUrl = "Images/add.gif"; |
if (((DataRowView)dataItem.DataItem)["resource_type_picture"] == Convert.DBNull) |
{ |
rtypepic.ImageUrl = "Images/blanksmall.gif"; |
} |
else |
{ |
rtypepic.ImageUrl = "ResourceTypesPic.aspx?id=" + dataItem["resource_type_id"].Text; |
} |
} |
} |
I hope this will help you!
Amanda