C# 列表<&燃气轮机;结果是无效的;can';似乎没有。正确地添加
我有一个带有C# 列表<&燃气轮机;结果是无效的;can';似乎没有。正确地添加,c#,asp.net,list,add,C#,Asp.net,List,Add,我有一个带有复选框的GridView,我希望在选中的每一行中检索单元格[1]。列表总是以'null'结束。代码如下。我添加了一个字符串来显示输出,效果很好。所以我可能添加的不正确,但我不知道是什么。任何帮助都将不胜感激。干杯~ List<int> indices = new List<int>(); CheckBox cb = new CheckBox(); string text = ""; foreach (GridViewRow row in GV0.Rows)
复选框的GridView
,我希望在选中的每一行中检索单元格[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");
列表索引=新列表();
复选框cb=新复选框();
字符串文本=”;
foreach(GV0.Rows中的GridViewRow行)
{
if(((复选框)row.FindControl(“复选框1”)).Checked)
{
text+=行。单元格[1]。text;
index.Add(int.Parse(row.Cells[1].Text));
}
}
标签1.Text=文本;
会话[“索引列表”]=索引;
重定向(“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++;
}
}
}
List all=new List();
列表字段=新列表();
列表字段类型=新列表();
列表索引=新列表();
int-show=0;
如果(!Page.IsPostBack)
{
全部=(列表)会话[“全部”];
字段=(列表)会话[“字段”];
fieldtypes=(列表)会话[“fieldtypes”];
索引=(列表)会话[“索引列表”];
show=(int)会话[“show”];
}
int j=0;
列表重复=新列表();
for(int i=0;i
您在会话中使用键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;
}
}
公共列表索引
{
得到
{
var val=会话[“指示列表”]作为列表;
if(val==null)
{
val=新列表();
会话[“指示列表”]=val;
}
返回val;
}
设置
{
会话[“指示列表”]=值;
}
}
列表在哪里结束为空?在您给出的代码中,它肯定不会是。也许您的意思是列表最终为空?在您展示的代码中,列表永远不会为空。您是否已将GridView canuseraddrows设置为true?@JonSkeet在我重定向到的页面中,当我检索会话对象并使用其计数运行循环时,它会给我NullReferenceException,这意味着我的列表为空。但在调试过程中,它还表示原始页面上的原始列表也是空的。所以我猜这个错误很可能是由我上面发布的代码引起的。你在检查iPostBack吗?