vir
vir

Reputation: 1091

How to give image relative path in asp.net

I have folder structure like this for asp.net site :

enter image description here

now i run the site and result is like this :

enter image description here

here i am able to see the image "6".

now on click on : button , i am opening another page : dashboard.aspx:

$(document).ready(function () {
            $("#btnDashBoard").on("click", function () {
                debugger;
                window.location.href = "dashboard/dashboard.aspx";
                return false;
            });
        });

enter image description here here i am not able to see my image, why?

my site.master html is like this :

<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btnDashBoard").on("click", function () {
                debugger;
                window.location.href = "dashboard/dashboard.aspx";
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="btnDashBoard" type="button" value="button" />
        </div>
        <div>
            <img src="../Images/orderedList6.png" alt="" />
        </div>
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

Upvotes: 0

Views: 5482

Answers (2)

MarchalPT
MarchalPT

Reputation: 1576

I had a problem which was, my path was for example: https://mysite/projects/index

And my html stored in database had an image with this src: ~/Images/myimg.png

So the path i was getting for the image was:

https://mysite/projects/index/~/Images/myimg.png

I just removed the "~" from my source and everything was solved, it lead to image path which was: https://mysite/Content/Images/myimg.png

Hope this helps

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107626

You should let ASP.NET help you figure out the paths. There's a utility method on every Control named ResolveClientUrl(). If you pass it the virtual path to a file, it will resolve its full path.

You can use it right in the src attribute for your <img> tag:

<img src='<%= Path.ResolveClientUrl("~/Images/orderedList6.png") %>' alt="" />

Upvotes: 1

Related Questions