Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, April 16, 2019 5:19 PM
I am attempting to change the mouse cursor on the screen to a wait cursor while my asp.net page is doing something. I have tried the following code without success (in code behind):
Cursor.Current = Cursors.WaitCursor;
do something
Cursor.Current = Cursors.Default;
I also tried this:
Page.ClientScript.RegisterStartupScript(GetType(), "", "InvokeWaitCursor();", true);
do something
Page.ClientScript.RegisterStartupScript(GetType(), "", "InvokeDefaultCursor();", true);
and in aspx I have this:
<script type="text/javascript">
function InvokeWaitCursor() {
document.body.style.cursor = 'wait';
}
function InvokeDefaultCursor() {
document.body.style.cursor = 'default';
}
</script>
What am I missing?
All replies (3)
Wednesday, April 17, 2019 3:29 AM
Hi baldwinjohn,
According to your description and code, I've made a test and found that the function really works.
It seems that if your body do not have some child elements, the cursor will not change on the page.
I've simulated two buttons to change the cursor.
If there are child elements like divs on the page, when I click the button, the cursor will change to wait style when you horver to the div.
When you hover out of the divs, the cursor will remain default.
I guess that the cursor style will always be default if there is no child element.
So I suggest that you could add child elements on your body and set the cursor's style in your condition.
Here is my testing sample.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CursorWait.aspx.cs" Inherits="WebApplicationForm.CursorWait" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" Text="click1" OnClientClick="InvokeWaitCursor()" OnClick="btn_Click" />
<asp:Button ID="Button1" runat="server" Text="click2" OnClientClick="InvokeDefaultCursor()" OnClick="btn_Click1" />
<div style="height: 500px; width: 700px; background-color: aqua" id="test"></div>
<div style="height: 100px; width: 400px; background-color: red"></div>
</div>
<script type="text/javascript">
function InvokeWaitCursor() {
document.body.style.cursor = 'wait';
}
function InvokeDefaultCursor() {
document.body.style.cursor = 'default';
}
</script>
</form>
</body>
</html>
protected void btn_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "InvokeWaitCursor();", true);
}
protected void btn_Click1(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "InvokeDefaultCursor();", true);
}
result:
Best Regards,
Jenifer
Monday, April 22, 2019 4:52 PM
In my case I have just one button that creates a report and displays the Open/save prompt when it is ready. I got the wait cursor working by calling the function with onclientclick in html. But I cannot seem to get the default cursor reset once the report is made available. I have tried PageScript and ClientScript. Last thing I have tried is this:
ClientScript.RegisterClientScriptBlock(this.GetType(), "tmp", "function normalcursor() {document.body.style.cursor = 'default';}", true);
Tuesday, April 23, 2019 7:53 AM
Hi baldwinjohn,
baldwinjohn
In my case I have just one button that creates a report and displays the Open/save prompt when it is ready. I got the wait cursor working by calling the function with onclientclick in html. But I cannot seem to get the default cursor reset once the report is made available.
How is your report created and loaded?
In my opinion, when you click the button to load the report, you could add a class which setting the cursor to wait.
.wait, .wait * { cursor: wait; }
function cursorwait(e) {
document.body.className = 'wait';
}
For more about this, you could refer to this link: https://stackoverflow.com/a/4053808/10487763
Best Regards,
Jenifer