简体   繁体   English

如何将数据从一种形式放入另一种形式的列表框中

[英]How to put data from one form into a listbox in another

Okay so I'm adapting a C# program to an asp program and I have a main form which contains a list box and another which adds new information to the list box. 好的,所以我正在将C#程序改编为asp程序,我有一个主窗体,其中包含一个列表框,另一个包含向列表框中添加新信息的窗体。 I can fill in the 2nd form and hold values in Application["getData"]; 我可以填写第二个表单并保存Application["getData"]; but when I go to the other page I need to run the following code. 但是当我转到另一个页面时,我需要运行以下代码。

public void AddGig()
    {
        AddGigForm frm = new AddGigForm();

        if (Application["getData"] != null)
        {
            Application["saveData"] = Application["getData"];
            gigList.addGig(frm.GetData());


            UpdateListbox();
        }

I run into problems at gigList.addGig as it goes back to the method GetData() on the 2nd form. 回到第2种形式的GetData()方法时,我在gigList.addGig遇到了问题。 I just have no idea what else to use. 我只是不知道还有什么用。

GetData method: GetData方法:

public GigOpportunity GetData()
    {


            Application["GetData"] = new GigOpportunity
                (txtId.Text, gigDate.SelectedDate, txtVenue.Text, txtGenre.Text,
                Convert.ToDouble(txtCost.Text), Convert.ToInt32(txtCapacity.Text), chkHeadliner.Checked, txtMainAct.Text, chkEngineer.Checked);


            return new GigOpportunity(txtId.Text, gigDate.SelectedDate, txtVenue.Text, txtGenre.Text, Convert.ToDouble(txtCost.Text), Convert.ToInt32(txtCapacity.Text), chkHeadliner.Checked, txtMainAct.Text, chkEngineer.Checked);
    }

addGig method: addGig方法:

public void addGig(GigOpportunity gigOpportunity)
    {
        //Make sure a gig with this id does not already exist

        foreach (GigOpportunity g in gigList)
        {
            if (g.GigId == gigOpportunity.GigId)
            {
                throw new DuplicateIdException();
            }
        }

        gigList.Add(gigOpportunity);
    }

I understand now your problem. 我现在了解您的问题。 You musn't think like in windows form. 您不会像在Windows窗体中那样思考。 You declared those method inside other form. 您以其他形式声明了这些方法。 When you call it by assigning a new Form object you will not get the value inside as they have been disposed after you change the page. 当您通过分配一个新的Form对象调用它时,您将无法获得内部的值,因为它们在更改页面后已被丢弃。

So in your case: 因此,在您的情况下:

  if (Application["getData"] != null)
  {
        Application["saveData"] = Application["getData"];
        gigList.addGig((GigOpportunity)Application["getData"]);


        UpdateListbox();
  }

But I will suggest you to use Session object instead of Application object. 但我建议您使用Session对象而不是Application对象。 You can read more about it here 您可以在这里了解更多信息

So you have to do like this: 因此,您必须这样做:

  if (Session["getData"] != null)
  {
        Session["saveData"] = Session["getData"];
        gigList.addGig((GigOpportunity)Session["getData"]);


        UpdateListbox();
  }

You don't need to create the second form object AddGigForm and you must be sure to call your method GetData in the form where is it declared to assign your Session. 您无需创建第二个表单对象AddGigForm并且必须确保以声明它分配您的Session的形式调用方法GetData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM