ASP.NET linkbutton visible property issue
I am using a public variable named IsAdmin in the code behind the aspx page.
public partial class _news : System.Web.UI.Page
{
public bool IsAdmin = false;
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name.Contains("admin"))
{
IsAdmin = true;
}
else
{
IsAdmin = false;
}
}
And I am using the Visible = '<% # IsAdmin%>' property to assign to the panes that I want to show if the user is an admin in the aspx page design. It works strangely for the link buttons that I put on the repeater.
<asp:Panel ID="Panel1" runat="server" Visible='<%#IsAdmin%>'>
<asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl='<%# "news_edit.aspx? Action=edit&id=" + Convert.ToString( Eval("news_id")) %>Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl='<%# "news.aspx?Action=delete&id=" + Convert.ToString( Eval("news_id")) %>'>Delete</asp:LinkButton>
</asp:Panel>
and it works fine, however outside the repeater I added another link without a panel
<asp:LinkButton ID="LinkButton4" runat="server" PostBackUrl="~/news_edit.aspx?action=new" Visible='<%#IsAdmin%>'>Add New Item</asp:LinkButton>
but the visible property doesn't work on it! I tried to put it in a panel and set the visible property, but that didn't work either.
So, I have the following doubts
1) what is the problem? 2) what is the technical name when we use links like "<% # IsAdmin%>" on the design page 3) Does the page load before the page is rendered after the page is rendered?
thanks
a source to share
<%# %>
is the syntax used to access data fields. Since you are likely to bind the repeater control at some point, these expressions will be appreciated.
Since you are most likely not invoking data binding on the panel and communication buttons outside of the relay, these expressions will not be processed. You can probably change them to something like
<%= IsAdmin.ToString() %>
and get the desired result.
To learn more about the differences, check out this excellent blog post .
Also, the page is loaded before the page is rendered. Page rendering is the last thing that happens in the life cycle of an ASP.Net page.
a source to share