Changing Image in Repeater based on file type

Sherry Collins asked on June 5, 2015 16:33

Good day all

I have a task to pull the last N documents that the logged in user has shared with other users. I am bringing back this dataset with a sql query and using a repeater control. In addition to listing the documents the Business wants to show an icon for each type of document. For example if the document is a pdf then they want the pdf icon, and so on. I don't know if this can be done with an ASCX transformation, so I have opted to write a custom function where I would pass the Documenttype (pdf, docx, etc) and let it return the filename like so "~\Images\pdf.jpg"

Is there anyway I can return the image filename to the GetImage function.

<%# GetImage(CustomFunctionCall(Eval("DocumentType"))) %>

Is there possibly a better way to do this?

Any help is appreciated.

thanks.

Correct Answer

Rui Wang answered on June 5, 2015 22:40

Sherry, can you just do it like this

<img src='/images/<%# ((string)Eval("DocumentType")).Replace(".","") %>.gif' alt="icon" />

<%# ((string)Eval("DocumentType")).Replace(".","") %> will give you extensions like pdf, rtf, etc. Then you just need a list of .gif for all the types stored in a images folder.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 5, 2015 18:17

Sherry, check out the Transformation Methods. More specifically this method <%# GetFileIcon(Eval("FileExtension")) %>. You could do a substring method to get the extension out of your URL pretty easy.

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 5, 2015 18:51

I am not getting anything back from this function.

    <div class="DocImage" >
<a href="<%# GetDocumentUrl() %>" class="blogPostImage"><%# GetFileIcon(Eval("doc")) %></a>
</div>

Am I doing something wrong?  Where does it look for icons.
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 5, 2015 20:06

Whats in your "doc" column? Have you tried just <%# GetFileIcon("doc") %>? What you have for syntax, is looking for a column in the datatable named "doc", that won't work unless that's a valid column.

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 5, 2015 20:11

I have tried it that way and still getting nothing. Is there some setting somewhere where it looks for images. Otherwise, how does it know where my image is stored? Am I able to set where it looks for icons?

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 5, 2015 21:01

This is what I'm seeing with the Inspector

<i class="icon-file-default icon-file-default" aria-hidden="true"></i>

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 5, 2015 23:34

Thanks Rui, that worked.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.