在asp.net中的某些条件下使链接无法点击

时间:2014-05-23 09:45:23

标签: c# asp.net datagrid

在我的ascx文件中,我有一个这种格式的数据网格:

<Columns>
                <asp:TemplateColumn SortExpression="companyName" HeaderText="Company Name">
                    <HeaderStyle Width="40%" />
                    <ItemTemplate>
                        <table cellSpacing="0" cellPadding="0" width="100%" border="0">
                            <tr>
                                <td width="95%">
                                    <asp:LinkButton CssClass="LinkButton" id="btnView" Runat="server" CommandName="ViewDetails" CommandArgument="<%# GetViewUrl((System.Data.Common.DbDataRecord)Container.DataItem) %>">
                                        <span ID="SpanTitle" Runat="server">
                                            <%# DataBinder.Eval(Container.DataItem,"companyName") %>
                                        </span>
                                    </asp:LinkButton>
                                    <asp:Label CssClass="DGNormal" ID="lblStatus" Runat="server" text='<%# StatusToText((int) DataBinder.Eval(Container.DataItem,"statusId")) %>'/>
                                    <asp:Image visible='<%# (bool) DataBinder.Eval(Container.DataItem,"WMCCMRated") %>' ID="imgProfiled" Runat="server" ImageUrl="~/images/WMCCMRated.gif" AlternateText="This company has been competency profiled by WMG" />
                                    <asp:Image Visible='<%# !(Convert.IsDBNull(DataBinder.Eval(Container.DataItem,"feedback")))%>' ID="feedback" Runat="server" ImageUrl="~/images/feedback.gif" AlternateText="The number of feedback this company has been received is ->" ImageAlign="Bottom" Height="18px" Width="12px"/>
                                    <asp:Label CssClass="NormalBold" ID="lblFeedback" Runat="server" text='<%# DataBinder.Eval(Container.DataItem,"feedback") %>' ForeColor="Navy"/>
                                </td>
                            </tr>
                            <tr>
                                <td>

因此companyName将是可点击的。 现在在我的代码隐藏中,我检查了这个条件:

// bind the data to the datagrid
            dgCompanies.PageSize = pageSize;
            dgCompanies.DataSource = rdr;

               int j = 0;
               foreach (DataGridItem item in dgCompanies.Items)
               {
                   HtmlGenericControl name = (HtmlGenericControl)item.Cells[j].FindControl("SpanTitle");
                   string drstring = name.InnerHtml.Trim();
                   if (checkfunction(drstring))
                   {

在这里,如果条件满足,我希望链接不可点击,我该如何归档?

2 个答案:

答案 0 :(得分:0)

这是Disables the linkbutton

/// <summary>
/// Disables the link button.
/// </summary>
/// <param name="linkButton">The LinkButton to be disabled.</param>
public static void DisableLinkButton(LinkButton linkButton)
{
    linkButton.Attributes.Remove("href");
    linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
    linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";
    if (linkButton.Enabled != false)
    {
       linkButton.Enabled = false;
    }

    if (linkButton.OnClientClick != null)
    {
        linkButton.OnClientClick = null;
    }
}

同时检查how-to-enable-or-disable-linkbutton

答案 1 :(得分:0)

如果以下代码

,请将您的服务器端功能放入其中
btnView.Enabled = false;

根据您的要求

((System.Web.UI.WebControls.LinkButton)item.FindControl("btnView")).Enabled= false;