c# - 如何防止我的文本框在回发时清除文本。

标签 c# asp.net visual-studio textbox postback

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

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

问题是,一旦应用程序执行完毕,应用程序(我相信)会刷新页面并清除 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;
              }
          }
      }
}

最佳答案

是的。如果 IsPostBack 为真,您将必须确定计算状态并在 Page_Load 内填充控件:

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

您也可以将 Initialize() 移动到 else 子句中。

关于c# - 如何防止我的文本框在回发时清除文本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8011555/

相关文章:

c# - WPF ComboBox 自动完成中的 SelectedItem

asp.net - 如何以编程方式查找 ASP.NET App_Data 文件夹路径

asp.net - LocalSystem 与 System 与 Local System Windows 系统帐户

c++ - OpenCV findContours函数问题

javascript - Visual Studio IDE 无法识别 Javascript 中的 EJS 变量

c# - 总安装系统内存 - 跨平台解决方案

C# 如何显示像 : 这样的对象列表

c# - 是否有适用于 Android 平板电脑的 c# IDE?

asp.net - 在 aspx 页面上打印 configurationmanager.appsettings 变量?

xml - 错误 : Can only generate one of classes or datasets