【问题标题】:Disable Select on a ListView在 ListView 上禁用选择
【发布时间】:2011-04-01 00:37:46
【问题描述】:

我想根据行中的其他数据禁用 ListView 的 Select 命令。例如,如果 UserStatus 为“T”,我想将“选择”超链接灰显并阻止选择该行。

我通过 RowCreated 事件中的以下语句在 GridView 中完成了同样的事情。但是,我无法为 ListView 重写该代码。

CType(e.Row.Controls(0), WebControl).Attributes("disabled") = "true"

<asp:listview runat="server" id="ListView">

<itemtemplate>
  <tr id="rowUsers" runat="server">
    <td><asp:linkbutton id="btnEdit" runat="server" text="Select" onclick="btnEdit_Click" /></td>
    <td><asp:label id="UserNameLabel" runat="server" text='<%# Bind("UserName") %>' /></td>
    <td><asp:label id="UserStatusLabel" runat="server" text='<%# Bind("UserStatus") %>' /></td>
  </tr>
</itemtemplate>

生成的输出...

<tr id="ListView_rowUsers_0">
  <td><a id="ListView_btnEdit_0" href="javascript:__doPostBack('ListView$ctrl0$btnEdit','')">Select</a></td>
  <td><span id="ListView_UserNameLabel_0">Adams,John P</span></td>
  <td><span id="ListView_UserStatusLabel_0">T</span></td>
</tr>

【问题讨论】:

    标签: asp.net listview


    【解决方案1】:

    最好使用 ItemDataBound 事件并执行 FindControl("btnEdit") 并简单地设置 Enabled 属性。

    如何使用ListView ItemDataBound事件。

    【讨论】:

      【解决方案2】:

      在您的项目模板中,您可以使用数据绑定语法来禁用按钮

      <ItemTemplate>
          <asp:LinkButton id="btnEdit" runat="server" text="Select" 
                        Enabled=<%# Eval("UserStatus") == "T" %> />
      </ItemTemplate>
      

      【讨论】:

      • 谢谢。虽然我使用了上面 gbs 的答案,但我确信数据绑定语法也可以。我挂断了 ItemCreated 事件,因为它适用于 GridView。
      【解决方案3】:

      试试

      CType(e.Item.Controls(0), WebControl).Attributes("disabled") = "disabled"
      

      在 ListView 的ItemCreated

      【讨论】:

      • 无法识别 e.row 和 ListView 的 WebControl。我尝试了 e.Item 和 LiteralControl,但没有运气。
      猜你喜欢
      • 2017-07-15
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      相关资源
      最近更新 更多