【问题标题】:Make link unclickable under some condition in asp.net在asp.net中的某些条件下使链接不可点击
【发布时间】:2014-07-12 14:52:02
【问题描述】:

在我的 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))
                   {

这里,如果条件满足,我希望链接是不可点击的,我怎么存档呢?

【问题讨论】:

  • btnView.Enabled = false; 你在搜索这样的东西吗?
  • 我不知道,我应该把它放在哪里以及如何在满足条件的情况下应用它?
  • 把它放在你的 if 在服务器端。正是我写的代码
  • 您还有一页后面有相同代码但没有控件 ID 的页面吗?你能分享你的项目吗?我会审查并更正它

标签: c# asp.net datagrid


【解决方案1】:

这是来自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

【讨论】:

  • 我不想禁用所有链接。只有一个at条件满足,其他链接应该正常工作。
【解决方案2】:

把你的服务器端函数放在if下面的代码里面

btnView.Enabled = false;

根据您的要求

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

【讨论】:

  • 那么 buttonView = true 又如何呢?因为某些链接需要工作,只有在 if 条件下才能使其无法点击
  • 它说 btnView 在当前上下文中不存在。
  • 您要禁用的链接按钮的 id 是什么?
  • 其实我试过 item.FindControl("btnView")).Enabled= false;但我仍然可以点击它。
  • 试试这个((System.Web.UI.WebControls.LinkButton)dgCompanies.Item[0].FindControl("btnView")).Enabled= false; 其中0是项目的索引
猜你喜欢
  • 2017-09-06
  • 2017-06-23
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多