日期:2014-05-18  浏览次数:20533 次

帮帮小弟!! gridview问题!!1
protected void gv_IsPopular_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {

  LinkButton up =e.Row.FindControl("linkbtn_Up") as LinkButton;
  LinkButton down = e.Row.FindControl("linkbtn_down") as LinkButton;
  if (whetherLine == 1)
  {
  up.Enabled = false;
  }
  whetherLine++;
  if (whetherLine == a.Rows.Count)
  {
  down.Enabled = false;
  }
  }
  }
提示: if (whetherLine == 1)
  {
  up.Enabled = false; 未将对象引用设置到对象的实例
  }

------解决方案--------------------
没获取到。
------解决方案--------------------
LinkButton up =e.Row.FindControl("linkbtn_Up") as LinkButton; 
--
看看是不是ID写错了
------解决方案--------------------
不要写在RowDataBound事件里,建议在绑定完GridView之后添加以下代码:
C# code

foreach(GridViewItem gvi in this.gv_IsPopular.Items)
{
LinkButton up = (LinkButton)gvi.FindControl("linkbtn_Up"); 
LinkButton down = (LinkButton)gvi.FindControl("linkbtn_down"); 
if (whetherLine == 1) 
{ 
      up.Enabled = false; 
 } 
 whetherLine++; 
 if (whetherLine == gvi.Count) 
 { 
      down.Enabled = false; 
 } 
}

------解决方案--------------------
linkbtn_Up是在ItemTemplate还是EditItemTemplate中的,如果是在EditItemTemplate中,楼主这样得不到
------解决方案--------------------
把代码帖完整
linkbtn_Up是在ItemTemplate还是EditItemTemplate中
------解决方案--------------------
LinkButton up =e.Row.FindControl("linkbtn_Up") as LinkButton;
up肯定没有找到,最好在使用之前判断一下是否null然后再处理
否则就会"未将对象引用设置到对象的实例 "

------解决方案--------------------
把ASPX 页代码贴一下

顺便把代码也改一下
C# code

            LinkButton up =e.Row.FindControl("linkbtn_Up") as LinkButton; 
            LinkButton down = e.Row.FindControl("linkbtn_down") as LinkButton; 
            if (whetherLine == 1) 
            { 
                if(up!=null)
                  up.Enabled = false; 
            } 
            whetherLine++; 
            if (whetherLine == a.Rows.Count) 
            { 
                if(down!=null)
                    down.Enabled = false; 
            }