【问题标题】:Add to ArrayList from different WebForms从不同的 WebForms 添加到 ArrayList
【发布时间】:2017-06-08 19:28:27
【问题描述】:

我在 webform1 中有 2 个 Web 表单我有 2 个按钮(添加和 Show_WebForm2)和一个文本框。在 WebForm1 中,当用户在文本框中键入名称并单击添加按钮时,名称将添加到 ArrayList。当用户在 WebForm1 中输入完名称后,他们会打开 WebForms2。在 WebForms2 中还有一个文本框和一个添加按钮。当用户在此 TextBox 中键入名称时,我希望将其添加到我在 WebForm1 中使用的同一个 ArrayList 变量中。我怎样才能做到这一点?

    public ArrayList FullNameApplicantArray = new ArrayList();
    protected void Button1_Click(object sender, EventArgs e)
    {
        FullNameApplicantArray.Add("(" + TextBox1.Text +")");
        Session["ListOfApplicants"] = FullNameApplicantArray[0];

        foreach (var item in Session["ListOfApplicants"].ToString())
        {
            Debug.Write(item);
        }
        Debug.WriteLine("");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/WebForm2.aspx");

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        FullNameApplicantArray.Add("(" + TextBox1.Text + ")");
        Session["ListOfApplicants"] = FullNameApplicantArray[0];

        foreach (var item in Session["ListOfApplicants"].ToString())
        {
            Debug.Write(item);
        }
        Debug.WriteLine("");
    }

【问题讨论】:

    标签: c# html asp.net .net


    【解决方案1】:

    该代码存在许多问题。当用户单击添加时,首先是从会话中检索列表,而不是存储到会话中。将项目添加到列表后,我们需要更新会话。

    WebForm1

    protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList FullNameApplicantArray = (ArrayList)Session["FullNameApplicantArray"] ?? new ArrayList();
        FullNameApplicantArray.Add("(" + TextBox1.Text + ")");
    
        foreach (var item in FullNameApplicantArray)
        {
            Debug.Write(item);
        }
        Debug.WriteLine("");
        Session["FullNameApplicantArray"] = FullNameApplicantArray;
    }
    
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/WebForm2.aspx");
    }
    

    WebForm2

    protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList FullNameApplicantArray = (ArrayList)Session["FullNameApplicantArray"] ?? new ArrayList();
        FullNameApplicantArray.Add("(" + TextBox1.Text + ")");
    
        foreach (var item in FullNameApplicantArray)
        {
            Debug.Write(item);
        }
        Debug.WriteLine("");
        Session["FullNameApplicantArray"] = FullNameApplicantArray;
    }
    

    还要考虑到 Debug.Write(item) 的输出窗口取决于 VS 的配置方式。

    【讨论】:

      【解决方案2】:

      您的代码中已经有了解决方案,只需跨不同的 WebForm 使用会话。

      在第二个 WebForm 页面中,在点击事件中,只需将会话分配给数组, 添加到数组中,然后保存到会话中。

      protected void Button2_Click(object sender, EventArgs e)
      {
          ArrayList FullNameApplicantArray2 = new ArrayList();
          FullNameApplicantArray2 = Session["ListOfApplicants"];
          FullNameApplicantArray2.Add("(" + TextBox1.Text + ")");
          Session["ListOfApplicants"] = FullNameApplicantArray2;
      
          foreach (var item in Session["ListOfApplicants"].ToString())
          {
              Debug.Write(item);
          }
          Debug.WriteLine("");
      }
      

      同样在您的 Button1_click 中,您需要保存整个列表而不是仅保存第一个元素:

      protected void Button1_Click(object sender, EventArgs e)
      {
          FullNameApplicantArray.Add("(" + TextBox1.Text +")");
          Session["ListOfApplicants"] = FullNameApplicantArray;
      
          foreach (var item in Session["ListOfApplicants"].ToString())
          {
              Debug.Write(item);
          }
          Debug.WriteLine("");
      }
      

      【讨论】:

        【解决方案3】:

        您需要在第二个表单上创建 ArrayList 的第二个实例,并将其分配给会话中保存的内容。

        public ArrayList FullNameApplicantArray = (ArrayList)Session["ListOfApplicants"];
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            FullNameApplicantArray.Add("(" + TextBox1.Text + ")");
            Session["ListOfApplicants"] = FullNameApplicantArray;
        
            foreach (var item in Session["ListOfApplicants"].ToString())
            {
                Debug.Write(item);
            }
            Debug.WriteLine("");
        }
        

        【讨论】:

          猜你喜欢
          • 2013-09-14
          • 1970-01-01
          • 2011-07-25
          • 2012-03-14
          • 2022-01-22
          • 1970-01-01
          • 2015-03-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多