在事件中需要获取Datagrid第一列的超级链接列的值,但是当想获取此列值时一直提示为将对象引用到实例中的错误:
前台代码:
<asp:HyperLinkColumn Text="thcod" Target="_blank" DataNavigateUrlField="thcod" DataNavigateUrlFormatString="thDetail.aspx?thcod={0}"
DataTextField="thcod" SortExpression="thcod" HeaderText="编号"></asp:HyperLinkColumn>
后台代码是:
Response.Write(DirectCast(e.Item.Cells(0).FindControl(0), HyperLink).Text)

解决方案 »

  1.   

    如果
    Response.Write(DirectCast(e.Item.Cells(0).Controls(0), HyperLink).Text)
    不行
    试试
    Response.Write(DirectCast(e.Item.Cells(0).Controls(1), HyperLink).Text)
      

  2.   

    我的Datagrid的第一列是超级链接列,此列数据榜定的是数据库中的一个字段。现在想编辑时获得此列的值。但是Response.Write(DirectCast(e.Item.Cells(0).Controls(0), HyperLink).Text)读取该列时一直提示:未为将对象引用到实例中的错误很奇怪,现在调试又好了,不知道怎么会这样
      

  3.   

    其实这就是不用模板列的缺点,我们只能通过Controls(index)来找控件,有时候随便一个标记在里面,我们的控件的顺序就会发生改变,用Controls(0)就未必可靠了
      

  4.   

    >>>Response.Write(e.Item.Cells(0).Text)
      

  5.   

    简易转成模板列,使用DirectCast(e.Item.Cells(0).FindControl("MyHyperlInkID"), HyperLink)