e.Item.Cells[1]  结合controls[n]怎么用? 不明白Controls[n]中 n的含义,如果cell[5]也就是第六列里面有很多控件, 
且有不同类型重复的控件呢,这种秩序是按相同类型的控件的秩序来计算,还是按所有类型的控件的先后顺序来算呢?      LinkButton lbt2 = (LinkButton)e.Item.Cells[1].Controls[1];
            if (lbt2 != null)
            {
                lbt2.Text = "hello!";
                lbt2.Attributes.Add("onclick", "return confirm('kkek1')");
            }
 
运行时是正常的,但是把里面的这个代码写成: LinkButton lbt2 = (LinkButton)e.Item.Cells[1].Controls[0];出问题了,错误说:Unable to cast object of type 'System.Web.UI.WebControls.HyperLink' to type 'System.Web.UI.WebControls.LinkButton'. 
怎么回事?

解决方案 »

  1.   

    e.Item.Cells[1].Controls[0]       
    cells[1]表示获取列的值就是第一列,Controls[0]表示在第一列中得到的是第一个控件
      

  2.   


    不好意思,上面cell[1]都改成 [5]
      

  3.   

    你是模板列,必须是这样写:
    LinkButton lbt2 = (LinkButton)e.Item.Cells[1].FindControl("控件名称");
    而非模板列,必须是这样写:
    LinkButton lbt2 = (LinkButton)e.Item.Cells[1].Controls[0]; 
    Controls[0],其中0是Cells[1]单元格第一个控件,若是1是Cells[1]单元格第二个控件,依此类推!!
      

  4.   


    LinkButton lbt = ((LinkButton)(e.Item.Cells[5].Controls[0]))
    这样就能找到
      

  5.   

    多个括号LinkButton lbt = (LinkButton)(e.Item.Cells[5].Controls[0])
      

  6.   


     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    LinkButton lbt = (LinkButton)(e.Item.Cells[5].Controls[0])}
      

  7.   

    顶,一列里可以有多个控件,像取消,更新就经常放一列,通过Controls[0]来区分
      

  8.   

    大问题来了 LinkButton lbt2 = (LinkButton)e.Item.Cells[5].Controls[1];是可以的但是 LinkButton lbt2 = (LinkButton)e.Item.Cells[5].Controls[0];却不可以,提示说Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.