Search code examples
htmlparameterscode-behind

Pass Dynamic String to HTML from CodeBehind


I have a dynamic string that gets the server map path, lets call it strPath. I'm trying to pass the value of this string to html by using <%strPath%> however it doesnt work. Any help would be greatly appreciated.

Dont mind the string here is static, once I can successfuly show the picture I'll make it dynamic.

       protected void Page_Load(object sender, EventArgs e)
        {
            string imagePath = Server.MapPath("../Uploads/" + "10.jpg");
        }




<asp:Image runat="server" ImageUrl="<%imagePath%>" />

Solution

  • Why not use an <asp:Image> and set the ImageUrl property on server side?

    <html xmlns="http://www.w3.org/1999/xhtml">
       <head runat="server">
         <title></title>
       </head>
      <body>
       <form id="form1" runat="server">
          <div>
            <asp:Image runat="server" ID="dynamicImage">
          </div>
      </form>
      </body>
    </html>
    

    Then on your code behide:

    protected void Page_Load(object sender, EventArgs e)
        {
            dynamicImage.ImageURL = http://YOURSITEURL/Images/YOURIMAGE.jpeg
        }