Alexander C.
Alexander C.

Reputation: 1181

Conditional statement in GridView to show or hide HyperlinkField data

My code:

<asp:HyperLinkField Text="Static<br />Map" HeaderText=""               
           DataNavigateUrlFields="PWSNO,TAG_NO"                 
           Visible = '<%# Eval("Status").ToString() == "Inactive" ? false : true %>'                
            DataNavigateUrlFormatString="index.aspx?No={0}&tag={1}" 
            ItemStyle-HorizontalAlign="Center" Target="_blank" />

What I'm trying to do is if "Status" property returns value of "Inactive" I don't want a link in that column to be created.

But with my existing code I'm getting an error stating

Error Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.

Upvotes: 0

Views: 1209

Answers (1)

Dave Zych
Dave Zych

Reputation: 21887

You need to use a TemplateField which has a DataBinding event.

<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="">
    <asp:Hyperlink Text="Static<br />Map" Target="_blank"
     Visible = '<%# Eval("Status").ToString() == "Inactive" ? false : true %>'></asp:Hyperlink>
</asp:TemplateField>

You'll have to manually set the NavigateUrl.

Upvotes: 1

Related Questions