0
我在使用我的代码时遇到了一些麻烦。 我试图为每种情况展示不同的图片。 但代码似乎有缺陷,我很新的C#和ASP.net,所以我很容易迷路。 下面是崩溃的代码的所述部分:RowIndex超出范围
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string Class = (e.Row.FindControl("txtClass") as TextBox).Text;
HtmlControl htmctrl = e.Row.FindControl("imgid") as HtmlControl;
switch (Class)
{
case "A1":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Blue.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
case "A2":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Green.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
case "A3":
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\Box_Red.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
default:
{
string Logo = @"C:\Users\Rudra\documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Images\not-found.png";
htmctrl.Attributes.Add("src", Logo);
break;
}
}
}
的崩溃发生在第一行GridViewRow row = GridView1.Rows[e.RowIndex];
。 RowIndex
未被识别(不显示在智能感知上)。 如果你能看到我出错的地方,为什么这会崩溃,如何解决它我会永远感激。
您可以注释掉该行'GridViewRow行= GridView1.Rows [e.RowIndex]'你有一定要采取,除了用行索引赋值之外,你不会在'OnRowDataBound'中的任何地方使用实例变量'row'。 – Prabhat
非常感谢!我想我会看到它出错的地方。 –