How to enable and disable the hyperlink in gridview?

Ranjith picture Ranjith · Jun 25, 2012 · Viewed 12.1k times · Source

I have a hyperlink in the template field. I want to enable and disable the hyperlink based on its value. Let's say if Id is "ABC" I want to disable the hyperlink. I tried the code below but it didnt work for me.

Enabled='<%# Convert.ToString(Eval("Id"))!= "ABC" ? true: false %>'

I tried the following in the code behind:

protected void gridResult_RowDataBound(object sender, GridViewRowEventArgs e) { 
    if (e.Row.RowType == DataControlRowType.DataRow)
    {  
        HyperLink status = (HyperLink)e.Row.Cells[2].Controls[0];
        if (status != null && status.Text == "ABC") {
            status.Enabled = false; 
        }
    }
}

But it is returning null every time.

    <asp:TemplateField HeaderText="Id">
                        <ItemTemplate>
                            <% if ( WebApp.Common.Auth.Admin() ) { %>
                                <a href="../../Edit/Default.aspx?<%= WebApp.Edit.Default.P_ID %>=<%# DataBinder.Eval(Container.DataItem, "Id") %>&amp;r=<%= buildPostBackPortion() %>"><%# DataBinder.Eval(Container.DataItem, "Id") %> Enabled='<%# Convert.ToString(Eval("Id"))!= "ABC" ? true: false %>'
</a>
                            <% } else { %>
                                <%# DataBinder.Eval(Container.DataItem, "Id") %>
                            <% } %>
                        </ItemTemplate>
                        </asp:TemplateField>

Answer

Trisped picture Trisped · Jun 25, 2012

When I need to do something as you describe I use the following:

ASPX:

<asp:TemplateField HeaderStyle-CssClass="cell-action"  ItemStyle-CssClass="cell-action">
    <ItemTemplate>
        <asp:HyperLink ID="viewHyperLink" runat="server" Text="View" />
        <asp:Label ID="messageLabel" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

C#:

protected void reportedIssuesGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    RowDataType row;
    HyperLink viewHyperLink;
    Label messageLabel;

    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem is RowDataType) {
        row = (RowDataType)e.Row.DataItem;
        viewHyperLink = (HyperLink)e.Row.FindControl("viewHyperLink");  //Gets the HyperLink
        messageLabel = (Label)e.Row.FindControl("messageLabel");    //Gets the Label
        if (row.Id != "ABC")
        {
            viewHyperLink.Visible = true;
            viewHyperLink.NavigateUrl = "~/Edit/Default.aspx?P_ID" + row.Id;

            messageLabel.Visible = false;

        }
        else
        {
            viewHyperLink.Visible = true;

            messageLabel.Visible = true;
            messageLabel.Text = row.Id;
        }
    }
}

Where RowDataType is the name of the type of the row data.

If you need additional assistance or have more questions then please ask. If this solves your issue then please accept.
FYI: a 25% acceptance rate is REALLY bad. You should try to get that up by accepting answers.

Update: Changed code to use messageLabel.