This one hit me some time ago but was easy to work around. Today I had to face it in a ListView's LayoutTemplate:
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
<% if ( ApplicationUser.IsCurrentUserTheAdmin )
{ %>
<hr />
<asp:ImageButton ID="SwitchAddSectionButton" runat="server" OnClick="SwitchAddSectionButton_Click"
ImageUrl="~/Resources/item_add.gif" />
<% } %>
</LayoutTemplate>
As you can see there is a dynamic part of the template which is available to users with sufficient priviledges.
Although google provides a lot of articles referening the issue, the solution I've came up is amazingly simple:
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
<div runat="server">
<% if ( ApplicationUser.IsCurrentUserTheAdmin )
{ %>
<hr />
<asp:ImageButton ID="SwitchAddSectionButton" runat="server"
OnClick="SwitchAddSectionButton_Click"
ImageUrl="~/Resources/item_add.gif" />
<% } %>
</div>
</LayoutTemplate>
3 comments:
You could also try extending the listview and implementing the following method:
protected override void CreateLayoutTemplate()
{
base.CreateLayoutTemplate();
if (this.Controls.Count == 1)
this.Controls[0].DataBind();
}
This allows you to use databinding syntax within the LayoutTemplate (which seems less of a hack).
Thank you! Works fine!
Nice one :)
Post a Comment