2
votes

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.

3
something like "prove this as the answer" would be nice :)user57508

3 Answers

9
votes

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!

0
votes

Sometimes you cannot set control properties with <%# %> and you have to resort to using OnItemDataBound(...) to get a reference to the control and set its Visible property there. Another thing that sometimes can be an issue is nested quotes, but in your sample code I don't see that as a problem. If your real code includes nested quotes, like Visible="<%# CanApprove(Eval("ID"))%>" then that may be your real issue. You can get around this by using single quotes and alternating with double quotes.

0
votes

This worked great for me too... thanks!!

<asp:Label runat="server" ID="lblLocaton" Text='<%# String.Format("{0}, {1}", Eval("City"), Eval("Region.Code")) %>'  Visible="<%# ShowLocation() %>" />

AND

MfnPresenter.Website.Presenters.IInstituitonListView.ShowLocation


    Get

    End Get
    Set(ByVal value As Boolean)

        'Used by visibility binding expression on lblLocation control inside dlFinancialInsitution

    End Set
End Property