为什么这不起作用?

时间:2011-02-04 18:12:31

标签: c# list session gridview

我有一个类,它将充当变量来存储来自文本框的数据:

public class Business
{
    Int64 _businessID = new Int64();
    int _businessNo = new int();
    string _businessName;
    string _businessDescription; 

    public Int64 BusinessID
    {
       get { return Convert.ToInt64(_businessID.ToString()); }
    }

    public int BusinessNo
    {
       get { return _businessNo; }
       set { _businessNo = value; }
    }

    public string BusinessName
    {
       get { return _businessName; }
       set { _businessName = value; }
    }

    public string BusinessDescription
    {
       get { return _businessDescription; }
       set { _businessDescription = value; }
    }

然后我将代码存储到文本框中的数据到会话和列表中(可能有很多企业一次上传到数据库) - 数据库目前无法解决。然后,我想将存储在会话中的业务列表显示在gridview上:(b =类业务)

List<Business> businessCollection = new List<Business>();

protected List<Business> GetBusinesses()
{
   return (List<Business>)Session["Business"];
}

protected void btnRow_Click(object sender, EventArgs e)
{
   if (Session["Business"] != null)
      businessCollection = (List<Business>)Session["Business"];

   Business b = new Business();

   b.BusinessNo = Convert.ToInt32(txtBNo.Text);
   b.BusinessName = txtBName.Text;
   b.BusinessDescription = txtBDesc.Text;

   businessCollection.Add(b);

   GridView1.DataSource = GetBusiness();
   GridView1.DataBind();
}

似乎没有将列表添加到gridview,有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

调试代码并确保if (Session["Business"] != null)实际评估为true。

如果为false,则表示您要添加一个永远不会从GetBusinesss

返回的列表

如果没有更多信息,您可以像这样重写:

    List<Business> businessCollection = new List<Business>();

    protected List<Business> GetBusinesses()
    {
        if (Session["Business"] == null) 
            return businessCollection;
        else 
            return (List<Business>)Session["Business"];
    }

    protected void btnRow_Click(object sender, EventArgs e)
    {            
        Business b = new Business();

        b.BusinessNo = Convert.ToInt32(txtBNo.Text);
        b.BusinessName = txtBName.Text;
        b.BusinessDescription = txtBDesc.Text;

        var currentCollection = GetBusinesses();

        currentCollection.Add(b);

        GridView1.DataSource = currentCollection;
        GridView1.DataBind();
    }

我个人不会这样做,因为您似乎需要Session["Business"]的作业,但我不想改变代码的逻辑。

<强>更新

我想用我想要完成的想法来更新它。

    protected List<Business> GetBusinesses()
    {
        if (Session["Business"] == null) 
            Session["Business"] = new List<Business>();
        return (List<Business>)Session["Business"];
    }

    protected void btnRow_Click(object sender, EventArgs e)
    {            
        Business b = new Business();

        b.BusinessNo = Convert.ToInt32(txtBNo.Text);
        b.BusinessName = txtBName.Text;
        b.BusinessDescription = txtBDesc.Text;

        var currentCollection = GetBusinesses();

        currentCollection.Add(b);

        GridView1.DataSource = currentCollection;
        GridView1.DataBind();
    }

答案 1 :(得分:0)

您似乎没有向Session["Business"]

分配任何内容

答案 2 :(得分:0)

您出现问题的可能性非常大,原因是您不一致地引用了业务列表对象。您已为此对象创建了一个访问器,因此无处不在。

此:

        if (Session["Business"] != null)
            businessCollection = (List<Business>)Session["Business"];

应该是:

        var businessCollection = GetBusiness();

注意var的使用:我怀疑将businessCollection定义为成员变量是问题的一部分。在任何情况下,如果您的意图是将列表存储在会话中,那么设计是不好的。所以我也会删除businessCollection的成员声明,​​并坚持使用本地范围的变量。