Asp – Databind Function to Controls Visible Property Does Not Work

asp.net

I'm using databinding to set the visible property on a control as such:

Control on Page:

<asp:LinkButton ID="ApproveTimeLink" runat="server"  Visible="<%# CanApprove() %>"> Approve Time</asp:LinkButton>

Code on CodeBehind:

Protected bool CanApprove()
{
  return false;
}

As you can see the control should not show, yet still does. I'm not getting any errors, and I'm baffled as to why this does not work.

Thanks for the help.

Best Answer

all you have to this is the following

protected void Page_Load(object sender, EventArgs e)
{
    this.DataBind();
}
public bool CanApprove()
{
    return false;
}

then you can use this method on the asp-control as you mentioned before!

but be we aware! Every property of the page has to be not null, otherwise the databind will fail with an exception!

Related Topic