stonypaul
stonypaul

Reputation: 695

Conditionally removing the HyperLink from a HyperLinkField in a GridView based on its Text value

The following GridView binds to a sql datasource and displays a table with a variable amount of rows. The first column is a HyperLinkField and in the last row this value is TOTAL which is not required to be a HyperLink. Is there a way of referencing the column explicitly by it's text value of TOTAL and removing the HyperLink?

    <asp:GridView runat="server" ID="grdSuspDiag" CssClass="dataTable fullWidth" OnRowDataBound="grdSuspDiag_RowDataBound" DataSourceID="odsSuspDiag" AutoGenerateColumns="False" >
        <Columns>
            <asp:HyperLinkField DataTextField="Site" HeaderText="" DataNavigateUrlFields="patListLink" DataNavigateUrlFormatString="{0}" ItemStyle-Width="220px" />
            <asp:BoundField DataField="Diagnosis" HeaderText="No. Referrals" />
            <asp:TemplateField HeaderText=" " >
                <ItemTemplate >&nbsp;</ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ca" HeaderText="No. New" />
            <asp:BoundField DataField="NoCA" HeaderText="No. Non-New" />
            <asp:BoundField DataField="Susp" HeaderText="No. Still Expected" />
            <asp:TemplateField HeaderText=" " >
                <ItemTemplate >&nbsp;</ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="PCa" HeaderText="% New Diagnosed" />
            <asp:BoundField DataField="PnoCa" HeaderText="% Non-New" />
            <asp:BoundField DataField="PSusp" HeaderText="% Suspected" />
        </Columns>
    </asp:GridView>

Upvotes: 0

Views: 1391

Answers (2)

Haseeb Asif
Haseeb Asif

Reputation: 1786

You can make it a template field and add condition in it.

<asp:TemplateField HeaderText=" " >
    <ItemTemplate >
    <%# Convert.ToString(Eval("pastListLink")).Equals("Total") ? Eval("pastListLink"): string.Format("<a href='http://www.google.com'>{0}</a>", Eval("pastListLink")) %>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 2

Andrei
Andrei

Reputation: 56726

Maybe in this case it is worth switching to template field, i.e. instead of HyperLinkField use something like this:

<asp:TemplateField HeaderText="" ItemStyle-Width="220px">
    <ItemTemplate>
        <asp:HyperLink runat="server"
                       Text='<%# Eval("Site") %>'
                       NavigateUrl='<%# Eval("patListLink") %>'
                       Visible='<%# Eval("Site").ToString() != "TOTAL" %>' />
        <asp:Label Text='<%# Eval("Site") %>'
                   Visible='<%# Eval("Site").ToString() == "TOTAL" %>' />
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 3

Related Questions