Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to show images in ASP image Control. I want to access images from "_layouts/images/myfolder/image.gif", like this:

Image1.ImageURL = "_layouts/images/myfolder/image.gif"; It does not work. Is there any other way to show images in ASP Image Control from _layout folder ?

share|improve this question
    
kindly add some detail, like which version of sharepoint you are using and where you are using ASP Image Control (i assume it's visual webpart but still ..) and the type of the solution - sandbox or farm –  Diptarag Nov 21 '12 at 9:39
    
Its SP2010 and i am using ASP image control within .ascx Control.Its farm solution. –  SPBeginer Nov 21 '12 at 11:10
add comment

2 Answers

up vote 0 down vote accepted

this is easy:

Image img = new Image();
img.ImageUrl = "/_layouts/images/imageyouwant.gif";
Controls.Add(img);

sharepoint can be fussy sometimes with the naming of the file extension. You can try the following: rename the image file from .gif to .GIF so then you can do:

Image img = new Image();
img.ImageUrl = "_layouts/images/myfolder/image.GIF";
Controls.Add(img);

if it doesnt show up or if like your case you have created new folders in 12hive within the image folder you then need to check to see if the folder security inheritence is set properly.

goto the following:

12hive -> Templates -> images -> find your folder where the file is stored within in your cas "myfolder" -> right click the folder -> select properties -> select security tab -> select advanced -> check "replace permission entries on all child objects with the entries shown here that apply to child objects" -> click ok -> do iisreset

that should do the trick ;)

if it doesnt can you check to see the groups or user names within the security tab? so you would need to do the same as above but this time on the image file rather than the folder. take a note of groups and names. You can either post it here or you can goto the image folder and select any image that is working and check their group or usernames to see if its the same and what is missing :)

hope it helps :)

share|improve this answer
    
Thanks! Actually i have created .ascx Control and under page load event i put the code that you write above. Now i want to show image within table in .ascx file and i am doing like this: <td><%=img%></td>. but it gives following error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Any idea what is wrong ?? –  SPBeginer Nov 21 '12 at 10:44
    
for your control block i presum you have <%= change that to <%# , in your page load add this Page.Header.DataBind(); for more info on this look here leedumond.com/blog/… –  ali Sharepoint Nov 21 '12 at 11:05
    
Thanks again! I have changed as you told <td><%#img%></td>. also i have put Page.Header.DataBind(); in my control's page load event. But now it throws NullReferenceException: "Object reference not set to an instance of an object". Need more help from you ?? –  SPBeginer Nov 21 '12 at 12:59
    
without code i wouldnt know what is throwing that error, it seems like your calling some control that doesnt exist yet. You should open another question for that error so i would be able to see the issue properly and also others could assist or edit your question to include the error and code, Have you debugged your code to see where its failing? –  ali Sharepoint Nov 21 '12 at 13:27
    
Sorry, The above error is solved. that was my mistake actually. But still i am getting same error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Even after Changes that you told. I have changed <td><%#img%></td>. also i have put Page.Header.DataBind(); in my control's page load event. –  SPBeginer Nov 21 '12 at 14:17
add comment

I think your path to the image is not really correct, try like that: "~/_layouts/images/myfolder/image.gif"

share|improve this answer
    
it is correct as "/_layouts/myfolder/image.gif"; works for me just fine –  ali Sharepoint Nov 21 '12 at 10:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.