Search code examples
c#asp.nettelerikrotator

How to display different type of images in ad rotator in aspx page


I have an Ad Rotator and the File contents are coming from a table column, the problem is sometime the File columns contents are image, sometimes text file or the column is empty. My question is how do I display a static image when there are no attachment images or the attachment is there but it's not an image, its a .txt file or .doc file.

<telerik:RadRotator ID="rrotAttachmentImages" runat="server"                                                      
  RotatorType="CoverFlowButtons"
Height="250px" Width="700px"
    ItemHeight="225px" ItemWidth="700px"                                                   
 ScrollDuration="100" >
 <ItemTemplate>
      &nbsp;&nbsp;&nbsp;&nbsp;                                                  
 <asp:CheckBox ID="chkImageDefault" runat="server" 
  Text="Default Image" Visible='<%# Eval("Default").ToString() == "1" %>'></asp:CheckBox>                                                  
 <asp:Label ID="lblImages_Comments" runat="server" Text="Comments:"></asp:Label>
<telerik:RadTextBox ID="rtxtImageComments" runat="server" Width="300px" Text='<%# Eval("Comments") %>' Enabled="false" ></telerik:RadTextBox>
<asp:Image runat="server" ID="Image" ImageUrl='<%# Eval("File") %>' AlternateText="Image"></asp:Image>   </ItemTemplate>  </telerik:RadRotator>

c# code:

    //get file attachments                     
  IQueryable<TableName> IqryAttachments = DAL.GetFileAttachments(recordid, "somepagename");
                    rrotAttachmentImages.DataSource = IqryAttachments;
                    rrotAttachmentImages.DataBind(); 

Solution

  • solved it using ItemDataBound event:

    Aspx code:

        <telerik:RadRotator ID="rrotAttachmentImages" runat="server"                                                      
      RotatorType="CoverFlowButtons"
    Height="250px" Width="700px"
        ItemHeight="225px" ItemWidth="700px"                                                   
     ScrollDuration="100"
    onitemdatabound="rrotAttachmentImages_ItemDataBound" >
     <ItemTemplate>
          &nbsp;&nbsp;&nbsp;&nbsp;                                                  
     <asp:CheckBox ID="chkImageDefault" runat="server" 
      Text="Default Image" Visible='<%# Eval("Default").ToString() == "1" %>'></asp:CheckBox>                                                  
     <asp:Label ID="lblImages_Comments" runat="server" Text="Comments:"></asp:Label>
    <telerik:RadTextBox ID="rtxtImageComments" runat="server" Width="300px" Text='<%# Eval("Comments") %>' Enabled="false" ></telerik:RadTextBox>
    <asp:Image runat="server" ID="Image" ImageUrl='<%# Eval("File") %>' AlternateText="Image"></asp:Image>   </ItemTemplate>  </telerik:RadRotator>
    

    C# code for ItemDataBound Event:

     protected void rrotAttachmentImages_ItemDataBound(object sender, RadRotatorEventArgs e)
        {
            #region Fixing Document Icon
    
            Image imgImage = e.Item.FindControl("Image") as Image;
            string strFile = imgImage.ImageUrl; 
            bool booImage = false;
    
            if (string.IsNullOrEmpty(strFile))
            {
                imgImage.ImageUrl = "~/somefolder/NoImage.jpg";
            }
            else
            {
                if (strFile.ToLower().EndsWith(".jpeg")) booImage = true;
                    if (strFile.ToLower().EndsWith(".jpg")) booImage = true;
                    if (strFile.ToLower().EndsWith(".gif")) booImage = true;
                    if (strFile.ToLower().EndsWith(".bmp")) booImage = true;
                    if (strFile.ToLower().EndsWith(".png")) booImage = true;
                    if (!booImage)
                    {
                        if (strFile.ToLower().EndsWith(".txt") || strFile.ToLower().EndsWith(".doc")
                            || strFile.ToLower().EndsWith(".docx")
                            || strFile.ToLower().EndsWith(".xls") || strFile.ToLower().EndsWith(".xlsx"))
                        {
                            imgImage.ImageUrl = "~/somefolder/DocumentIco.png";
                        }
                        else
                        {
                            imgImage.ImageUrl = "~/somefolder/NoImage.jpg";
                        }
                    }
            }
    
    
            #endregion
        }