asp.net - How can I hide the loading panel of a master page in a child page - Stack Overflow

admin2025-05-01  1

Hello all I am having a master page where I have a Menu and one of the item is as follows

 Dim mnuItem As New RadMenuItem()
 mnuItem.Text = displayname.DisplayName
 mnuItem.Attributes.Add("onclick", "showLoadingPanel()")
 mnuItem.NavigateUrl = "GetFile.aspx"

On my master page I have the following div

 <div runat="server" id="loadingPanel" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 9999;">
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
            <img src="../Images/ajax-loader_clock.gif" alt="Loading..." />
        </div>
    </div>

The JavaScript to show the loading panel is as follows

function showLoadingPanel() {
     var loadingPanel = document.getElementById("loadingPanel");
     if (loadingPanel) {
          loadingPanel.style.display = "block";
     }
    }

In my child page I am trying as follows by adding an attribute to hide it but couldn't

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GetFile.aspx.vb" Inherits="GetFile" MasterPageFile="~/Site.master" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .hide {
            display: none;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
</asp:Content>

In my VB file on page load I am handling as follows but couldn't hide it

Dim loadingPanel As HtmlGenericControl = CType(Master.FindControl("loadingPanel"), HtmlGenericControl)
loadingPanel.Attributes.Add("class", "hide")

In my GetFile.aspx here is my code

If the page is getting redirected that is fine but I am doing this Public

Sub DeliverFile(ByVal Page As System.Web.UI.Page,
            ByVal Data() As Byte, ByVal Type As String,
            ByVal Length As Integer,
            Optional ByVal DownloadFileName As String = "")

        Page.Response.Clear()
        Page.Response.ContentType = ContentType
        'Page.Response.AddHeader("Content-Disposition", "SOA.pdf")
        'Page.Response.AddHeader("Content-Disposition", $"attachment; filename={fileName}.pdf")
        Page.Response.AddHeader("Content-Disposition", "attachment; filename=" & DownloadFileName & ".pdf")

        Page.Response.AddHeader("Content-Length", Length)
        Page.Response.BinaryWrite(Data)
        Page.Response.End()
    End Sub

Hello all I am having a master page where I have a Menu and one of the item is as follows

 Dim mnuItem As New RadMenuItem()
 mnuItem.Text = displayname.DisplayName
 mnuItem.Attributes.Add("onclick", "showLoadingPanel()")
 mnuItem.NavigateUrl = "GetFile.aspx"

On my master page I have the following div

 <div runat="server" id="loadingPanel" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 9999;">
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
            <img src="../Images/ajax-loader_clock.gif" alt="Loading..." />
        </div>
    </div>

The JavaScript to show the loading panel is as follows

function showLoadingPanel() {
     var loadingPanel = document.getElementById("loadingPanel");
     if (loadingPanel) {
          loadingPanel.style.display = "block";
     }
    }

In my child page I am trying as follows by adding an attribute to hide it but couldn't

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GetFile.aspx.vb" Inherits="GetFile" MasterPageFile="~/Site.master" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .hide {
            display: none;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
</asp:Content>

In my VB file on page load I am handling as follows but couldn't hide it

Dim loadingPanel As HtmlGenericControl = CType(Master.FindControl("loadingPanel"), HtmlGenericControl)
loadingPanel.Attributes.Add("class", "hide")

In my GetFile.aspx here is my code

If the page is getting redirected that is fine but I am doing this Public

Sub DeliverFile(ByVal Page As System.Web.UI.Page,
            ByVal Data() As Byte, ByVal Type As String,
            ByVal Length As Integer,
            Optional ByVal DownloadFileName As String = "")

        Page.Response.Clear()
        Page.Response.ContentType = ContentType
        'Page.Response.AddHeader("Content-Disposition", "SOA.pdf")
        'Page.Response.AddHeader("Content-Disposition", $"attachment; filename={fileName}.pdf")
        Page.Response.AddHeader("Content-Disposition", "attachment; filename=" & DownloadFileName & ".pdf")

        Page.Response.AddHeader("Content-Length", Length)
        Page.Response.BinaryWrite(Data)
        Page.Response.End()
    End Sub
Share Improve this question edited Jan 3 at 3:52 Developer asked Jan 2 at 19:07 DeveloperDeveloper 8,70143 gold badges136 silver badges252 bronze badges 4
  • Is the "hide" css class name even applied? – Mikhail Commented Jan 2 at 22:01
  • The master page and the child page do NOT load separate, and they are combined into one page, and ONLY AFTER all server processing has 100% completed, is THEN and ONLY then is the a single WHOLE page of the two parts sent to the client side as ONE page! So, you can't for example have the master page have some "wait while we load child page" animation, and then expect the child page to then load, and then have the wait gif animation stop. There are not two pages send from the server - master + child come down as a combed single whole page. You can't have a please wait for a single page load. – Albert D. Kallal Commented Jan 3 at 0:05
  • So, you can have code in the child page hide, or show a panel or div in the master page, but as per above, you can't use this for a "please wait" while the page loads - that will not work. So, it not clear what you final goal here is, but if the child page is to load some GrdiView, or some content that takes quite a bit of time? Then you have to let the "one" page (that is the combined master + child) 100% be sent to the client side, and then say JavaScript can then show a "please wait", and then do some kind of post-back to say load the GridView, or whatever content takes a long time. – Albert D. Kallal Commented Jan 3 at 0:09
  • As a result of the above, you might want to explain what you final goal is here, since I suspect what you are attempting will not produce the results you want, since master + child are rendered as one single page, and sent to the client side as one single page - not two separate transmissions from the web server. – Albert D. Kallal Commented Jan 3 at 0:11
Add a comment  | 

1 Answer 1

Reset to default 0

As pointed out in comments, both the master page, and the child page are combined into ONE page, and ONE transmission to the client side.

So, the page can take 1 second, or 20 seconds, and the end user will not see ANY content transmitted from the server. Only after 100% of the code behind is completed, and ONLY after both master + child page are combined into ONE payload that is THEN sent in ONE operation to the client side.

As such, you can't of course have some "please wait" while the page loads and is rendered, since ZERO content is transmitted from the server until such time all of the code behind runs and renders the page to be sent client side. This includes both master + child. They are transmitted in "one payload".

So, assuming you have this content in the master page:

(I have reduced the size, since with your z-index, I can't click on the buttons, so that's been removed also).

Hence this in master page:

<div runat="server" id="loadingPanel" clientidmode="static"
    style="display: none; position: fixed; top: 10%; left: 0; width: 10%; height: 10%; background-color: rgba(0, 0, 0, 0.5);">
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
            <asp:Image ID="Image1" runat="server"
                ImageUrl="~/Content/wait2.gif"
                AlternateText="Loading..."
                width="64px"
                />
        </div>
    </div>

Also, note closely in above, I have changed your image control from a client side control to a server side one. This allows use of "~/" which means root of the site. You can NOT use "relative" addressing for the master page images and content, since the master page as I pointed out is combined into ONE page, and that ONE page is the child page. So, relative addressing to images will CHANGE depending on the location of the child page! The current "relative" path names used are now the CHILD page for the MASTER page!

This means if you launch a child page nested in some folder, then ALL of the relative addressing you used in the master page will break!! Keep in mind that "~/" which means start from root ONLY works for server side controls, it does not work for client side JavaScript code.

So, now with above in master page, then say in our child page, we have this:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <asp:Button ID="Button1" runat="server" Text="Show panel"
        OnClientClick="myshow();return false"        
        />

    <br />
    <br />

    <asp:Button ID="Button2" runat="server" Text="Hide panel"
        OnClientClick="myhide();return false"
        />


    <script>

        function myshow() {
            alert('about to show')
            $('#loadingPanel').show()
        }


        function myhide() {
            $('#loadingPanel').hide()
        }

    </script>

</asp:Content>

And now the result is this:

Note also how I added clientidmode="static" to the div in the master page. If you don't add that, then the control ID as usual will be changed by .net due to the runat="server".

So, above shows a working bit of code that will show, and hide the div we have in the master page. But, as such, it not going to show some "loading" of the page to the client side, since 100% of the code, delays, and combine of the master + child occurs. Thus, all content, and rendering once done THEN is sent to the client side in one operation - thus removing any ability to show some "wait" based on server side content.

You have to let the content travel 100% to client side (to display the wait gif). Once the page is sent, loaded client side, THEN the animated gif will display. So, I suppose at that point, you could (have to) THEN trigger additional JavaScript to run, that can then in turn trigger additional server side code to run. So, you have to trigger the gif wait client side to display, and THEN call additional code which could/would trigger say some server code to load up the GridView or whatever it is that you want display (that presumably takes a long time).

Edit: User wants to hit a button, jump to another page

Ah, yes, then this is most certainly possible.

So, let's change your div back to what you had (with z-index etc.).

Hence, your div in the master page is thus this:

<div runat="server" id="loadingPanel" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 9999;"
    clientidmode="static"            
    >
    <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
        <asp:Image ID="Image1" runat="server"
            ImageUrl="~/content/wait2.gif"
            width="64px"
            />
    </div>
</div>

As noted, I used a asp.net image control, since the master page location will CHANGE the relative addressing. You can NOT use relative addressing in the master page markup, since you don't know which/when/where the nesting of the child page is going to be. So, the child page controls the relative path as nesting. If the child page is nested, then so are the references and path name(s) used in the master page. Use of a server control and "~/" thus means reference from the root of the project.

So, on page 1, I have this button and markup:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h3>Jump to another page - slow loading grid view</h3>

    <asp:Button ID="cmdJump" runat="server" Text="Show Grid of fighters"
        OnClick="cmdJump_Click"
        OnClientClick="mywaitshow()"
        CssClass="btn" />

    <script>

        function mywaitshow() {

            $('#loadingPanel').show()

        }

    </script>


</asp:Content>

So, now upon button click, the client side code runs, displays the wait gif.

And the code behind for that button is simply this:

Protected Sub cmdJump_Click(sender As Object, e As EventArgs)


    Response.Redirect("Fighters2.aspx")


End Sub

And the target page is this code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ID" CssClass="table" Width="100%">
    <Columns>
        <asp:BoundField DataField="Fighter" HeaderText="Fighter" />
        <asp:BoundField DataField="Engine" HeaderText="Engine" />
        <asp:BoundField DataField="Thrust" HeaderText="Thrust" />
        <asp:BoundField DataField="Description" HeaderText="Description" />
        <asp:TemplateField HeaderText="Preview">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Width="128px"
                    ImageUrl='<%# Eval("Imagepath") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And code behind for the target page is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid
    End If

End Sub

Sub LoadGrid()

    System.Threading.Thread.Sleep(3000) ' fake 3 second wait

    GridView1.DataSource = MyRst("SELECT * FROM Fighters")
    GridView1.DataBind()

End Sub

So, note the fake delay. And note how the "wait" gif will automatic disappear. This is due to the WHOLE page being sent back from the server, and until that happens, we see the current page "div". With a new fresh whole page, then no code is required to hide the div.

Hence, the effect is now this:

转载请注明原文地址:http://www.anycun.com/QandA/1746101323a91685.html