javasocute
javasocute

Reputation: 648

How to prevent my textbox from clearing text on postback.

I have a pretty interesting dilemma that is giving me a hurricane of a headache. I've seen a similar question asked here, but the user posted no code, so it was unresolved. This is a asp.net, sql server, and c# app.

In my application, I use JavaScript to display dummy data in a TextBox to entertain the user while a long process is running. (please note, this is a project, not a professional app).

The problem is, once the app is done executing, the app (I believe) refreshes the page and clears the TextBox. I want to prevent this from happening and continue to display the text after the program is completed.

My question is, where in the following code is the page being refreshed? How can I re-code it to prevent the text from being cleared?

I know there is no issue with the JavaScript or the .aspx page. I have not set or programmed any property to clear the text. If anyone could shed light on the issue, I would greatly appreciate it. If you need more code from me please let me know. I will be very active on this page until it is resolved. Thanks again!

public partial class SendOrders : System.Web.UI.Page
{
    protected enum EDIType
    {
        Notes,
        Details
    }

    protected static string NextBatchNum = "1";
    protected static string FileNamePrefix = "";
    protected static string OverBatchLimitStr = "Batch file limit has been reached.  No more batches can be processed today.";

    protected void Page_Load(object sender, EventArgs e)
    {
        Initialize();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    { }

    protected void btnExit_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }

    protected void Button_Click(object sender, EventArgs e)
    {
        PutFTPButton.Enabled = false;
        Thread.Sleep(3000);
        Button btn = (Button)sender;
        KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
        KaplanFTP.Transmit transmit = new KaplanFTP.Transmit();

        if (btn.ID == PutFTPButton.ID)
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Kaplan");
            FileInfo[] BatchFiles = bf.GetBatchFiles(dir);
            bool result = transmit.UploadBatchFilesToFTP(BatchFiles);

            if (!result)
            {
                ErrorLabel.Text += KaplanFTP.errorMsg;
                return;
            }

            bf.InsertBatchDataIntoDatabase("CTL");
            bf.InsertBatchDataIntoDatabase("HDR");
            bf.InsertBatchDataIntoDatabase("DET");
            bf.InsertBatchDataIntoDatabase("NTS");

            List<FileInfo> allfiles = BatchFiles.ToList<FileInfo>();
            allfiles.AddRange(dir.GetFiles("*.txt"));
            bf.MoveFiles(allfiles);

            foreach (string order in bf.OrdersSent)
            {
                OrdersSentDiv.Controls.Add(new LiteralControl(order + "<br />"));
            }

            btnExit.Visible = true;
            OrdersSentDiv.Visible = true;
            OrdersInfoDiv.Visible = false;
            SuccessLabel.Visible = true;
            NoBatchesToProcessLbl.Visible = true;
            BatchesToProcessLbl.Visible = false;
            PutFTPButton.Enabled = false;
            BatchesCreatedLbl.Text = int.Parse(NextBatchNum).ToString();
            Thread.Sleep(20000);

            if (KaplanFTP.errorMsg.Length != 0)
            {
                ErrorLabel.Visible = false;
                SuccessLabel.Visible = true;
                ErrorLabel.Text = KaplanFTP.errorMsg;
            }
        }
    }

    private void Initialize()
    {
          KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();

          if (!IsPostBack)
          {
              FileNamePrefix = bf.FileNamePrefix;
              NextBatchNum = bf.NextBatchNum;
              BatchesCreatedLbl.Text = (int.Parse(NextBatchNum) - 1).ToString();

              if (bf.CheckLocalForNewBatch() == true)
              {
                  NoBatchesToProcessLbl.Visible = false;
                  BatchesToProcessLbl.Visible = true;
                  if (int.Parse(NextBatchNum) >= 50)
                  {
                      ErrorLabel.Text += ErrorLabel.Text + OverBatchLimitStr;
                      ErrorLabel.Visible = true;
                      PutFTPButton.Enabled = false;
                  }
                  else
                  {
                      bf.ReadyFilesForTransmission();
                      ErrorLabel.Visible = false;
                      PutFTPButton.Enabled = true;
                      List<string[]> detStream = bf.GetBatchStream("DET");
                      List<string[]> hdrStream = bf.GetBatchStream("HDR");
                      OrdersInfoDiv.Visible = true;

                      DataTable dt = new DataTable();
                      dt.Columns.Add("ORDER NUMBER");
                      dt.Columns.Add("LINE NUMBER");
                      dt.Columns.Add("ITEM NUMBER/ISBN");
                      dt.Columns.Add("DESCRIPTION");
                      dt.Columns.Add("QUANTITY");
                      dt.Columns.Add("SHIPPING");

                      Dictionary<string, string> orderShip = new Dictionary<string, string>();

                      foreach (string[] hdrItems in hdrStream)
                      {
                          orderShip.Add(hdrItems[0], hdrItems[2]);
                      }
                      foreach (string[] detItems in detStream)
                      {
                          List<string> detLineList = new List<string>(detItems);
                          detLineList.Add(orderShip[detItems[0]]);
                          detLineList.RemoveAt(13);
                          detLineList.RemoveAt(12);
                          detLineList.RemoveAt(11);
                          detLineList.RemoveAt(10);
                          detLineList.RemoveAt(9);
                          detLineList.RemoveAt(8);
                          detLineList.RemoveAt(7);
                          detLineList.RemoveAt(4);
                          detLineList.RemoveAt(2);
                          detLineList[1] = detLineList[1].TrimStart('0');
                          detLineList[4] = detLineList[4].TrimStart('0');
                          dt.Rows.Add(detLineList.ToArray());
                      }

                      BatchDetails.DataSource = dt;
                      BatchDetails.DataBind();
                  }
              }
              else
              {
                  NoBatchesToProcessLbl.Visible = true;
                  BatchesToProcessLbl.Visible = false;
                  PutFTPButton.Enabled = false;
              }
          }
      }
}

Upvotes: 3

Views: 7214

Answers (2)

Deco
Deco

Reputation: 3321

Since you are handling the button click server side (I'm assuming the problem is happening once the user clicks the button?) there has to be a postback to handle it.

You could try putting your button and label into an updatepanel control - it uses AJAX to refresh its contents.

See this page for more information on updatepanels.

Upvotes: 0

drdwilcox
drdwilcox

Reputation: 3951

Yes. You will have to determine the state of your calculation and populate the control inside Page_Load in the case where IsPostBack is true:

protected void Page_Load(object sender, EventArgs e) {
   if (IsPostBack) {
      // re-populate javascript-fill UI
   } else {
   } 
   Initialize();
}

You can probably also move Initialize() into the else clause as well.

Upvotes: 3

Related Questions