GridView控件中子控件绑定问题
GridView控件中有一子控件(代码中为Label),要求根据某权限值的不同,在绑定时控制在页面上显示与否。
aspx:
<asp:GridView ID="gv_Storage" runat="server" Width="850px" DataKeyNames="Id" AutoGenerateColumns="False" AllowPaging="True" PageSize="20" OnRowDataBound="gv_Storage_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemStyle Width="150px" HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lb_Add" runat="server" Text='<%# "<A href=Orde_add.aspx?Id="+ DataBinder.Eval(Container.DataItem,"Id").ToString() +">添加购买</a>" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#:
protected void gv_Storage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Session["adm_Kind"].ToString() == "1")
{
Label lb_Add = (Label)e.Row.FindControl("lb_Add");
if (Session["adm_Kind"].ToString() == "1")
{
lb_Add.Visible = true;
}
else
{
lb_Add.Visible = false;
}
}
}
请教正确的写法!
------解决方案--------------------加个判断是否为数据行,
if (e.Row.RowType == DataControlRowType.DataRow)