为什么我的类声明列表在另一个方法中使用时变为空,而不是填充它的方法

时间:2022-09-25 14:43:07

I declared my list "lijstBijlagen" at the top of my class so I would be able to use it in all the methods of that class. Now the problem is that I fill this list up in the method "btnUpload_Click" but when I want to use it in the method btnSubmit_Click it becomes empty again. I left out my Page_Load code in the code underneath this text because I don't think you'll need it to solve the problem.

我在我的班级顶部声明了我的列表“lijstBijlagen”,所以我可以在该类的所有方法中使用它。现在的问题是我在方法“btnUpload_Click”中填写此列表,但是当我想在方法btnSubmit_Click中使用它时,它再次变为空。我在本文下面的代码中遗漏了我的Page_Load代码,因为我认为你不需要它来解决问题。

namespace Veiligheidsplan.WebUI
{
    public partial class MailPage : System.Web.UI.Page
    {
        private List<string> lijstBijlagen = new List<string>(); 


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        List<string> parts = txtOntvangers.Text.Split(',').Select(p => p.Trim()).ToList();
        MailService.SendMail(parts, txtOnderwerp.Text, txtBody.Text, lijstBijlagen);
        Server.Transfer("Homepage.aspx");
    }



    protected void btnUpload_Click(object sender, EventArgs e)
    {

        string bijlage = ddlBijlagen.SelectedItem.Text;
        string bijlagePad = "~/Veiligheidsplan/" + Session["IngelogdeGebruikersnaam"]+"/" + bijlage;
        lijstBijlagen.Add(bijlagePad);
        lblCommentaar.Visible = true;
        lblCommentaar.Text += ", "+bijlage;
    }
}

}

1 个解决方案

#1


You will need to persist your list every postback.
为什么我的类声明列表在另一个方法中使用时变为空,而不是填充它的方法

每次回发都需要保留列表。

#1


You will need to persist your list every postback.
为什么我的类声明列表在另一个方法中使用时变为空,而不是填充它的方法

每次回发都需要保留列表。