未将对象引用设置到对象的实例的问题
我的代码如下:
aspx:
……
<asp:HyperLink ID= "HyperLink4 " runat= "server " Text= "编辑 " NavigateUrl= ' <%# Eval( "ID ", "EditBlog.aspx?id={0} ") %> ' Visible= "false "> </asp:HyperLink> </td>
</tr>
</table> </td>
</tr>
<tr>
<td style= "background-color: #cccccc; height:1px; "> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs: protected void Page_Load(object sender, EventArgs e)
{
HyperLink HyperLink4 = (HyperLink)GridView1.FindControl( "HyperLink4 ");
if (User.Identity.Name == "aabbcc " || User.Identity.Name == "668811 ")
{
HyperLink4.Visible = true;
}
else
{
HyperLink4.Visible = false;
}
}
可为什么老提“未将对象引用设置到对象的实例。”?好像是找不到控件一样??
------解决方案--------------------protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink4 = (HyperLink)e.Row.FindControl( "HyperLink4 ");
if (User.Identity.Name == "aabbcc " || User.Identity.Name == "668811 ")
{
HyperLink4.Visible = true;
}
else
{
HyperLink4.Visible = false;
}
}
}