列表与LT;>最终为空;似乎不能。添加得当

时间:2013-01-25 17:13:46

标签: c# asp.net list add

我有GridView CheckBoxes,我希望在已检查的每一行中检索Cell[1]。该列表最终总是'null'。代码如下。我添加了一个字符串来显示输出,并且工作正常。所以我可能错误地添加它但我不知道是什么。任何帮助,将不胜感激。干杯〜

List<int> indices = new List<int>();
CheckBox cb = new CheckBox();

string text = "";
foreach (GridViewRow row in GV0.Rows)
{
    if (((CheckBox)row.FindControl("CheckBox1")).Checked)
    {
        text += row.Cells[1].Text;
        indices.Add(int.Parse(row.Cells[1].Text));
    }
}
Label1.Text = text;
Session["indicesList"] = indices;
Response.Redirect("About.aspx");

要重定向到的页面的代码:

        List<List<string>> all = new List<List<string>>();
        List<string> fields = new List<string>();
        List<Type> fieldtypes = new List<Type>();
        List<int> indices = new List<int>();
        int show = 0;

        if (!Page.IsPostBack)
        {
            all = (List<List<string>>)Session["all"];
            fields = (List<string>)Session["fields"];
            fieldtypes = (List<Type>)Session["fieldtypes"];
            indices = (List<int>)Session["indiceslist"];
            show = (int)Session["show"];
        }

        int j = 0;
        List<List<string>> dupes = new List<List<string>>();
        for (int i = 0; i < show; i++)
        {
            if (j < indices.Count)
            {
                if (int.Parse(all[i][0]) == indices[j])
                {
                    dupes.Add(all[i]);
                    j++;
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您在会话中使用indicesList键设置列表,但是您使用indiceslist键检索它(请注意字母“L”的大小写)。

我建议为您的列表创建一个从会话中获取和设置的属性。它使管理变得更加容易。

public List<int> Indices
{
    get
    {
        var val = Session["indicesList"] as List<int>;
        if(val == null) 
        {
            val = new List<int>();
            Session["indicesList"] = val;
        }
        return val;
    }
    set
    {
        Session["indicesList"] = value;
    }
}