skip to Main Content

I am using ASP.net Web forms

I have users register into my page

once they registered a 3rd pty code updates the database which can take between 1 sec and few minutes

I want to add a rotating spinner wheel while server is running and waiting for the 3rd pty response.

there is a loop with waiting to check the for 3rd pty result

what i want, is to have a spinner that shows the user that there is a process running in the backend

I tried to add asp:image with spinning gif, but when i call it it does not show till the process is finished

i wonder if there is an easy way to add the spinner

here is my code for reg.aspx

<div>
<div>Register</div>
<div>
    <asp:TextBox ID="FirstName" runat="server" placeholder="First Name"></asp:TextBox>
</div>
<div>
    <asp:TextBox ID="LastName" runat="server" placeholder="Last Name"></asp:TextBox>
</div>
<div>
    <asp:TextBox ID="PhoneNumber" runat="server" placeholder="Phone Number"></asp:TextBox>
</div>
<div>
    <asp:TextBox ID="Email" runat="server" placeholder="Email" TextMode="Email"></asp:TextBox>
</div>
<div>
    <asp:TextBox ID="Password" runat="server" placeholder="Password" TextMode="Password"></asp:TextBox>
</div>
    <div>
    <asp:Label ID="ErrorMessage" runat="server" Visible="false" Text=""></asp:Label>
</div>
<div>
    <asp:Image runat="server" ID="imgSpin" ImageUrl="~/images/spinner6.gif" Visible="true" />
</div>                    

<asp:Button ID="registerButton" runat="server" Text="Sign Up" OnClick="registerButton_Click" />

</form>
</body>

and here for reg.aspx.cs

protected void registerButton_Click(object sender, EventArgs e)
{
    RegisterUser();
}

   protected void RegisterUser()
   {
       imgSpin.Visible = true;
       
       string firstName = FirstName.Text;
       string lastName = LastName.Text;
       string phoneNumber = PhoneNumber.Text;
       string email = Email.Text;
       string password = Password.Text;

       Reg(firstName, lastName, phoneNumber, email, password);
       string resultCode = "*";
       bool bExit = false;


       while (!bExit)
       {
           switch (resultCode.ToUpper())
           {
               case "ERROR":
                   ErrorMessage.Text = "Invalid user ID or password. Please try again.";
                   ErrorMessage.Visible = true;
                   bExit = true;
                   break;


               case "OK":
                   Response.Redirect("job.aspx");
                   bExit = true;
                   break;

               default:
                   // Wait 2 seconds, then call the procedure again
                   System.Threading.Thread.Sleep(2000);
                   resultCode = Call3Pty(); 
                   break;
           }
       }

       imgSpin.Visible = false;
   }

GPT suggested adding 2 event to the Signup button but that never works, only client side gets triggered but the backend never did

<asp:Button ID="registerButton" runat="server" Text="Sign Up" OnClick="registerButton_Click" OnClientClick="showSpinner();" />

how can i add a simple spinner while my code running in the background

2

Answers


  1. Asp.NET (starting from v2.0) has a set of handy AJAX controls, ScriptManager, UpdatePanel, UpdateProgress, and more. Here is a sample script.

    register.aspx

    <form ID="Form1" runat="server">
    <asp:ScriptManager ID="scriptMgr1" runat="server" /><%-- must go before other controls --%>
    <asp:UpdatePanel ID="upPnl" runat="server" UpdateMode="Conditional">
    <asp:ContentTemplate>
    <asp:TextBox ID="userName" runat="server" />
    <%--Other form fields --%>
    <asp:Button ID="registerButton" runat="server" Text="Sign Up" OnClick="registerButton_Click" />
    </ContentTemplate>
    <asp:UpdateProgress ID="upProgress" runat="server" DisplayAfter="1000"><%-- 1000ms --%>
    <ProgressTemplate><%-- can be outside UpdatePanel --%>
    <img src="spin.gif" />
    </ProgressTemplate>
    </asp:UpdateProgress>
    </asp:UpdatePanel>
    </form>
    

    register.aspx.cs

    protected void registerButton_Click(object sender, EventArgs e)
    {
    RegisterUser();
    }
    
    Login or Signup to reply.
  2. Obviously when you modify controls on the web page, then the end user sees a browser wait (spin) icon. So, any controls, any image, or even making changes to some text box while the page is posted up on the server is not going to be seen by the end user until such time the WHOLE page makes the trip back to the end user’s browser. This includes script injection (registering scripts).

    In other words, code behind changes to the browser page are not seen until the WHOLE page comes back to the client side. In effect, code behind never interacts directly with the end user, but makes changes to the browser page while it is up on the server, and changes to the "DOM" are not seen by code behind until such time the code is 100% complete. Once the code 100% completes, then a whole new fresh copy of the page is sent back to the client side. Hence, setting some spinner or control with visible=true with code behind will not work, and as such, changes to the page are not seen until code 100% completes.

    You can use a update panel, or you can simply attach a client script function to the button press (standard buttons allow both a client side event, and a server side event). So, when you press the button, both client side code and server side code (the post back, and start of the so called "round trip", or better stated page lifecycle starts.

    So, in place of a update panel, some simple jQuery will suffice.

    So, say this test code:

    Markup:

            <asp:Button ID="Button1" runat="server" Text="Start the process"
                OnClick="Button1_Click"
                OnClientClick="$('#myspinarea').show();"
                CssClass="btn btn-dark"
                />
            <br />
    
            <div id="myspinarea" style="display:none">
                <h3>Please wait....</h3>
                <img src="../Content/wait2.gif"
                    width="82"
                    />
            </div>
    

    And code behind:

        protected void Button1_Click(object sender, EventArgs e)
        {
            // fake a long delay
            System.Threading.Thread.Sleep(3000);
        }
    

    And the result is now this:

    enter image description here

    Note close how we don’t have to re-hide the div area, since a WHOLE new fresh copy of the browser is sent when code behind is complete, and thus the spinner area hides automatic for us.

    Above assumes jQuery is available.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search