Gary Evans
Gary Evans

Reputation: 1880

Refreshing a textbox on screen

I have a process that runs a series of tasks and with each tasks updates a text box, my issue is however that the screen doesnt load until all the tasks are done, and I need to see them as they happen. In my old language of VBA this would have been DoEvents or Me.Repaint. What am I doing wrong causing my text box to not show what is sent to it each time something is sent to it?

This is in ASP.net and C#. This is the aspx page:-

<%@ Page Title="ASC Open" Language="C#" AutoEventWireup="true" CodeBehind="ASC_OSC.aspx.cs" Inherits="HICS.ASC_OSC" %>

<head runat="server">
    <title>HICS</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:BundleReference runat="server" Path="~/Content/css" />
</head>
<body style="padding-top: 5px">
    <form id="main" runat="server">
        <div class="container-fluid" style="padding-top: 0px">
            <h1>Open Access Supply Chain</h1>
            <hr />
            <div class="container-fluid">
              <div ID="ContRow" class="row">
                <div class="col-md-12">
                    <asp:TextBox ID="TxtStatus" runat="server" Height="80%" Width="80%" ReadOnly="true" Wrap="true" AutoPostBack="false" TextMode="MultiLine"></asp:TextBox>
                </div>
              </div>
            </div>
        </div>
    </form>
</body>

And this is the snippet of C# (not all showing as it is representative), it is TxtStatus that I care about right now.

using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace HICS
{
    public partial class ASC_OSC : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataReader RS;
            int     IntPort;
            int     IntEnvironment;
            bool    BlnOK;
            string StrEnvironmentName;
            string StrEnvironmentDB;
            Database DB_ASC = new Database();


            if (!IsPostBack)
            {
                BlnOK = true;
                TxtStatus.Text += "Getting Port: ";
                string StrReturn = home.Procs.UserSetting("ASC_Port");
                if (StrReturn == "" || StrReturn == "0")
                {
                    RS = home.DB_HICS.GetRS("SELECT MAX([ASC_Port]) FROM [Tb_Users]");
                    RS.Read();
                    IntPort = (RS.GetInt32(0) < 4444)? 4444 : RS.GetInt32(0) + 1;
                    home.Procs.UserSetting("ASC_Port", IntPort.ToString());
                    RS.Close();
                }
                else
                {
                    IntPort = Convert.ToInt32(StrReturn);
                }
                TxtStatus.Text += String.Concat(IntPort.ToString() , Environment.NewLine);

                TxtStatus.Text += "Getting Environment: ";
                IntEnvironment = 0;
                StrReturn = home.Procs.UserSetting("ASC_Environment");
                if (StrReturn == "" || StrReturn == "0")
                {
                    RS = home.DB_HICS.GetRS("SELECT MIN([ID]) FROM [Tb_ASC_Environments]");
                    RS.Read();
                    if (RS.IsDBNull(0))
                    {
                       BlnOK = false;
                    }
                    else
                    {
                        IntEnvironment = RS.GetInt32(0);
                        home.Procs.UserSetting("ASC_Environment", IntEnvironment.ToString());
                    }
                    RS.Close();
                }
                else
                {
                    IntEnvironment = Convert.ToInt32(StrReturn);
                    RS = home.DB_HICS.GetRS("SELECT COUNT([ID]) FROM [Tb_ASC_Environments] WHERE [ID] = " + IntEnvironment);
                    RS.Read();
                    if (RS.GetInt32(0)==0)
                    {
                        RS.Close();
                        RS = home.DB_HICS.GetRS("SELECT MIN([ID]) FROM [Tb_ASC_Environments]");
                        RS.Read();
                        if (RS.IsDBNull(0))
                        {
                            BlnOK = false;
                        }
                        else
                        {
                            IntEnvironment = RS.GetInt32(0);
                            home.Procs.UserSetting("ASC_Environment", IntEnvironment.ToString());
                        }
                    }
                    RS.Close();
                }
                TxtStatus.Text += String.Concat((BlnOK)?IntEnvironment.ToString():"Error", Environment.NewLine);

                if (BlnOK)
                {
                    RS = home.DB_HICS.GetRS("SELECT [Name],[Database] FROM [Tb_ASC_Environments] WHERE [ID] = " + IntEnvironment);
                    RS.Read();
                    StrEnvironmentName = RS.GetString(0);
                    StrEnvironmentDB = RS.GetString(1);
                    TxtStatus.Text += String.Concat("Environment Name: ", StrEnvironmentName, Environment.NewLine);
                    TxtStatus.Text += String.Concat("Environment Database: ", StrEnvironmentDB, Environment.NewLine);
                    TxtStatus.Text += "Connecting to ASC Database: ";

                }
            }
        }
    }
}

Upvotes: 0

Views: 365

Answers (1)

Filburt
Filburt

Reputation: 18061

You need to understand that a web page is fundamentally different from a desktop client application where you can DoEvents and Repaint whenever you need. ASP.NET is executed server side and thus performs a full round trip between submitting a page and processing events on the server.

If you start a long running task and want to get progress information, you need to use AJAX.

See Introduction to the UpdateProgress Control for a older concept

or

track the progress of a database save in a “live” progress bar asp.net mvc for a modern approach

... you'll have to make a choice for your specific scenario.

Upvotes: 2

Related Questions