How to Dynamically Add a Photo with Labels Underneath So Labels Don't Shift Up & Down

Jan Tervonen 20 Reputation points
2023-01-20T23:46:50.7266667+00:00

I have a simple HTML/ASPx page that loads an image, and then sets a caption underneath in the Page_Load method. The filename of the image is passed as a parameter. The height of the image is unknown at design time.

 When loading the page, the labels appear first at the top. After the image appears, then the labels shift underneath the image where I want them to be.

Is there a way to stop the labels from appearing until AFTER the image is shown? As is, the shift is distracting.

Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-01-23T06:15:10.4733333+00:00

    Hi @Jan Tervonen

    I would like to make two points:

    First: This paragraph of the code you gave is wrong:SetLabels(FileStr)

    Second: You can try the following layout:

    In ASPX page

    <div>
    <asp:Image ID="LargeImg" runat="server" />
    </div>
    <div>
    <asp:Label ID="TitleLbl" runat="server></asp:Label>
    </div>
    

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. SurferOnWww 4,711 Reputation points
    2023-01-22T02:16:33.3966667+00:00

    When loading the page, the labels appear first at the top. After the image appears, then the labels shift underneath the image where I want them to be.

    Can the following aspx.cs and aspx simulate what you say above?

    aspx.cs

    using System;
    
    namespace WebForms1
    {
        public partial class WebForm15 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = "ASP.NET MVC";
            }
        }
    }
    

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="WebForm15.aspx.cs" Inherits="WebForms1.WebForm15" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript">
            window.addEventListener('DOMContentLoaded', () => {
                let img = document.querySelector('img');
    
                setTimeout(() => {                
                    img.src = "images/image1.jpg";
                }, 1000);
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Image ID="Image1" runat="server" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </form>
    </body>
    </html>
    

    If so, remove the Label setting in the Page_Load and set the caption on the load event if the img element by JavaScript, as follows:

    aspx.cs

    using System;
    
    namespace WebForms1
    {
        public partial class WebForm15 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Label1.Text = "ASP.NET MVC";
            }
        }
    }
    

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="WebForm15.aspx.cs" Inherits="WebForms1.WebForm15" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript">
            window.addEventListener('DOMContentLoaded', () => {
                let img = document.querySelector('img');
                setTimeout(() => {                
                    img.src = "images/image1.jpg";
                }, 1000);
    
                img.addEventListener('load', () => {
                    let label = document.querySelector('span');
                    label.innerText = "ASP.NET MVC";
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Image ID="Image1" runat="server" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </form>
    </body>
    </html>
    
    0 comments No comments

  2. SurferOnWww 4,711 Reputation points
    2023-01-22T02:19:24.5333333+00:00

    When loading the page, the labels appear first at the top. After the image appears, then the labels shift underneath the image where I want them to be.

    Can the following aspx.cs and aspx simulate what you say above?

    aspx.cs

    using System;
    
    namespace WebForms1
    {
        public partial class WebForm15 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = "ASP.NET MVC";
            }
        }
    }
    

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="WebForm15.aspx.cs" Inherits="WebForms1.WebForm15" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript">
            window.addEventListener('DOMContentLoaded', () => {
                let img = document.querySelector('img');
    
                setTimeout(() => {                
                    img.src = "images/image1.jpg";
                }, 1000);
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Image ID="Image1" runat="server" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </form>
    </body>
    </html>
    

    If so, remove the Label setting in the Page_Load and set the caption on the load event if the img element by JavaScript, as follows:

    aspx.cs

    using System;
    
    namespace WebForms1
    {
        public partial class WebForm15 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Label1.Text = "ASP.NET MVC";
            }
        }
    }
    

    aspx

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="WebForm15.aspx.cs" Inherits="WebForms1.WebForm15" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script type="text/javascript">
            window.addEventListener('DOMContentLoaded', () => {
                let img = document.querySelector('img');
                setTimeout(() => {                
                    img.src = "images/image1.jpg";
                }, 1000);
    
                img.addEventListener('load', () => {
                    let label = document.querySelector('span');
                    label.innerText = "ASP.NET MVC";
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Image ID="Image1" runat="server" />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </form>
    </body>
    </html>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.