如何防止我的文本框在页面回传时清空文本。

3

我有一个非常有趣的困境,让我头痛不已。我在这里看到过类似的问题,但用户没有发布任何代码,所以问题未解决。这是一个asp.net、sql server和c#应用程序。

在我的应用程序中,我使用JavaScript在TextBox中显示虚拟数据,以便在长时间运行的过程中娱乐用户。(请注意,这是一个项目,而不是专业应用程序)。

问题是,一旦应用程序执行完成,应用程序(I believe)会刷新页面并清除TextBox。我想阻止这种情况发生,并在程序完成后继续显示文本。

我的问题是,在以下代码中,页面在哪里被刷新?如何重新编码以防止文本被清除?

我知道JavaScript或.aspx页面没有任何问题。我没有设置或编程任何属性来清除文本。如果有人能够解决这个问题,我将不胜感激。如果您需要更多的代码,请告诉我。直到问题解决之前,我将非常活跃在这个页面上。再次感谢!

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;
              }
          }
      }
}

我使用后端代码。但是所有的JavaScript都是将文本插入到文本框中,就这样而已。您想让我也发布那段代码吗? - javasocute
1
这段代码中是否有涉及到所提到的文本框?你正在使用UpdatePanels吗? - patmortech
文本框仅在JavaScript和HTML页面上引用。我没有使用更新面板。该页面上的文本框是上面发布的代码。问题在于刷新/回发在此页面上,而不是实际处理文本框的js或html。 - javasocute
作为一条建议,你真的需要重构这段代码。在当前状态下,这是一个意大利面式的噩梦。编写处理批处理文件处理和数据表细节的类会对你有很大帮助。 - jwheron
2个回答

3

是的。在IsPostBack为true的情况下,您需要确定计算的状态并在Page_Load中填充控件:

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

您可以将Initialize()方法放到else语句中。

这是个好主意。你会用JavaScript怎么写呢? - javasocute
你不能在 JavaScript 中编写这个。必须在 C# 代码后台中编写以通过 postbacks 持久化数据。 - drdwilcox
哈哈,我明白了。您先生真是个特*么的天才。非常感谢! - javasocute
谢谢你的赞美。这来自于许多、许多页的 C#。 - drdwilcox

0

由于您正在服务器端处理按钮单击(我假设问题发生在用户单击按钮后?),因此必须进行回传以处理它。

您可以尝试将按钮和标签放入updatepanel控件中-它使用AJAX刷新其内容。

有关updatepanel的更多信息,请参见此页面


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接