需要帮助循环遍历页面上的多个 gridviews c#.net
Need help looping through multiple gridviews on a page c#.net
我需要帮助迭代页面上的 2 个网格视图。最终我将在页面上总共有 6 个网格视图(或部分)。每个 gridview 都有相同的列,但每个都有不同的行数。迭代的 objective 是计算在当前 gridview 迭代中总 gridview 行中的一列中选择的单选按钮列表项的总数,因此信息可以显示在提交按钮点击的标签中,如下所示:
“第 1 部分的 14 个评分中有 10 个已检查。” 或者
“第 2 部分中的 11 个评级中的 11 个已被检查。” 或者无论如何。
我已经通过谷歌搜索得出了我目前拥有的代码,并关注了这个线程,包括聊天 How to use a for loop to modify multiple controls (gridview)
但是我不明白如何处理这行代码。省略号后面是什么??它适用于我的情况吗?
((GridView) Page.FindControl("GridView" + i)).Rows[1].........
当我添加 .Rows[j]
(或 1)时,.Rows
上会出现一条红色波浪线……并警告:system.web.ui.control 不会包含 'Rows' 的定义,并且找不到接受类型 system.web.ui.control 的第一个参数的扩展方法 'Rows'(是否缺少 using 指令或程序集引用?)。我确实有 using System.Linq
;我页面顶部的声明。
这是我通过 gridviews 进行计数的代码。 GridView id 是 GridView1、GridView2 等。GridView 使用 sqldatasource 填充并包含绑定字段和模板字段列。如果我 运行 通过 在浏览器中查看的代码 它会在这行代码中抛出错误“对象引用未设置为对象的实例”:
foreach (GridViewRow row in gv.Rows);
虽然在调试期间,它似乎识别了正确的 GridView,因为它获得了正确的行数并且选择了正确的单选按钮列表项。
代码:
//UPDATED code includes solution:
protected void CountSelectedRatings()
{
int count = 0;
int itemsSelected = 0;
string str = string.Empty;
int i = 1;
//loop through 6 gridviews
for (i = 1; i <= 6; i++)
{
//identify current grid to iterate through
GridView gv = (GridView)Page.FindControl("GridView" + i);
itemsSelected = 0;
if (gv != null)
{
foreach (GridViewRow row in gv.Rows)
{
count = gv.Rows.Count;
RadioButtonList rbl = row.FindControl("rblRating" + i) as RadioButtonList;
foreach (ListItem item in rbl.Items)
{
if (item.Selected == true)
{
//increment count
itemsSelected += 1;
switch (i)
{
case 1:
strRowCountMessage1 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
lblRowCountMessage1.Text = strRowCountMessage1;
break;
case 2:
strRowCountMessage2 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
lblRowCountMessage2.Text = strRowCountMessage2;
break;
//case 3:
// strRowCountMessage3 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage3;
// break;
//case 4:
// strRowCountMessage4 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage4;
// break;
//case 5:
// strRowCountMessage5 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage5;
// break;
//case 6:
// strRowCountMessage6 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage6;
// break;
}//end switch
}//end if item.Selected = True
}//end foreach ListItem
}//end foreach gridviewrow
}//end if gv !=null
}// end for loop i
GridView1.DataBind();
GridView2.DataBind();
//GridView4.DataBind();
//GridView5.DataBind();
//GridView6.DataBind();
}
我最初问题的解决方案是在 foreach 循环之前检查空 gridview。该部分代码的摘录如下。我修改了我的原始代码以反映最终代码,该代码使用单选按钮对总行数进行了计数并显示在标签中。
for (i = 1; i <= 6; i++)
{
//identify current grid to iterate through
GridView gv = (GridView)Page.FindControl("GridView" + i);
itemsSelected = 0;
if (gv != null)//<--
{
foreach (GridViewRow row in gv.Rows)
{//code continues here}}
我需要帮助迭代页面上的 2 个网格视图。最终我将在页面上总共有 6 个网格视图(或部分)。每个 gridview 都有相同的列,但每个都有不同的行数。迭代的 objective 是计算在当前 gridview 迭代中总 gridview 行中的一列中选择的单选按钮列表项的总数,因此信息可以显示在提交按钮点击的标签中,如下所示: “第 1 部分的 14 个评分中有 10 个已检查。” 或者 “第 2 部分中的 11 个评级中的 11 个已被检查。” 或者无论如何。
我已经通过谷歌搜索得出了我目前拥有的代码,并关注了这个线程,包括聊天 How to use a for loop to modify multiple controls (gridview)
但是我不明白如何处理这行代码。省略号后面是什么??它适用于我的情况吗?
((GridView) Page.FindControl("GridView" + i)).Rows[1].........
当我添加 .Rows[j]
(或 1)时,.Rows
上会出现一条红色波浪线……并警告:system.web.ui.control 不会包含 'Rows' 的定义,并且找不到接受类型 system.web.ui.control 的第一个参数的扩展方法 'Rows'(是否缺少 using 指令或程序集引用?)。我确实有 using System.Linq
;我页面顶部的声明。
这是我通过 gridviews 进行计数的代码。 GridView id 是 GridView1、GridView2 等。GridView 使用 sqldatasource 填充并包含绑定字段和模板字段列。如果我 运行 通过 在浏览器中查看的代码 它会在这行代码中抛出错误“对象引用未设置为对象的实例”:
foreach (GridViewRow row in gv.Rows);
虽然在调试期间,它似乎识别了正确的 GridView,因为它获得了正确的行数并且选择了正确的单选按钮列表项。
代码:
//UPDATED code includes solution:
protected void CountSelectedRatings()
{
int count = 0;
int itemsSelected = 0;
string str = string.Empty;
int i = 1;
//loop through 6 gridviews
for (i = 1; i <= 6; i++)
{
//identify current grid to iterate through
GridView gv = (GridView)Page.FindControl("GridView" + i);
itemsSelected = 0;
if (gv != null)
{
foreach (GridViewRow row in gv.Rows)
{
count = gv.Rows.Count;
RadioButtonList rbl = row.FindControl("rblRating" + i) as RadioButtonList;
foreach (ListItem item in rbl.Items)
{
if (item.Selected == true)
{
//increment count
itemsSelected += 1;
switch (i)
{
case 1:
strRowCountMessage1 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
lblRowCountMessage1.Text = strRowCountMessage1;
break;
case 2:
strRowCountMessage2 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
lblRowCountMessage2.Text = strRowCountMessage2;
break;
//case 3:
// strRowCountMessage3 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage3;
// break;
//case 4:
// strRowCountMessage4 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage4;
// break;
//case 5:
// strRowCountMessage5 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage5;
// break;
//case 6:
// strRowCountMessage6 = itemsSelected + " of " + count + " ratings in Section " + i + " are checked.";
// lblRowCountMessage3.Text = strRowCountMessage6;
// break;
}//end switch
}//end if item.Selected = True
}//end foreach ListItem
}//end foreach gridviewrow
}//end if gv !=null
}// end for loop i
GridView1.DataBind();
GridView2.DataBind();
//GridView4.DataBind();
//GridView5.DataBind();
//GridView6.DataBind();
}
我最初问题的解决方案是在 foreach 循环之前检查空 gridview。该部分代码的摘录如下。我修改了我的原始代码以反映最终代码,该代码使用单选按钮对总行数进行了计数并显示在标签中。
for (i = 1; i <= 6; i++)
{
//identify current grid to iterate through
GridView gv = (GridView)Page.FindControl("GridView" + i);
itemsSelected = 0;
if (gv != null)//<--
{
foreach (GridViewRow row in gv.Rows)
{//code continues here}}