【问题标题】:Postback using button in GridView使用 GridView 中的按钮回发
【发布时间】:2012-01-19 21:05:23
【问题描述】:

我在 ASP.NET GridView 中有以下内容:

<asp:TemplateField>
  <ItemTemplate>

    <asp:ImageButton ID="ibtnDown" runat="server" ToolTip="Down" CommandName="Down" 
      ImageUrl="~/images/arrow.png" Width="16px" Height="16px" 
      CommandArgument="<%# Container.DataItemIndex %>" />

  </ItemTemplate>
</asp:TemplateField>

它只是一个图像按钮,单击该按钮会引发 GridView 的 RowCommand 事件。这非常方便,因为它允许我使用 CommandName 和 CommandArgument 来确定单击的按钮以及单击的行。

但是,我发现 .NET ImageButton 的局限性在于它无法显示 CSS 精灵来更改鼠标悬停时的图像。至少我无法让它工作,但当然这并不排除它可能的某种方式。

无论如何,我想使用一个标准的 HTML 按钮标签,因为我知道如何让它与我的精灵一起工作。

<button name="btnDown" id="btnDown" type="submit" class="downArrow"></button>

但我不知道该怎么做是让按钮导致回发并引发 RowCommand 事件,但仍然允许我以某种方式访问​​ CommandArgument 和 CommandName 参数。

【问题讨论】:

    标签: asp.net gridview button postback


    【解决方案1】:

    是的,我首先将按钮设为服务器控件,然后使用其各种属性作为 commandName 和 CommandArgument 的替代。此外,我将使用 OnServerClick 事件代替 rowCommand。最后,我将使用 sender 对象将其解析为 hmt 按钮并从其属性中提取值。我想我没有迷路。

    <button runat="server" name="Down" id="btnDown" title="<%# Container.DataItemIndex %>" type="submit" class="downArrow" OnServerClick="ClickMe"></button
    

    【讨论】:

    • 这很棒!这有点不合常规,可能不适用于所有场景(例如,当您实际上需要将 title 属性用于其预期目的时),但它确实完成了工作。谢谢!
    【解决方案2】:

    您是否考虑过使用按钮字段来执行您希望执行的操作?

    看看下面的链接 ButtonFields within a GridView

    【讨论】:

      【解决方案3】:

      只需使用 (original post here):

      <asp:LinkButton ID="LinkButton1" runat="server">
          <div class="sprite-box_mac_osx_disc_button" />
      </asp:LinkButton> 
      

      当然可以使用你自己的css,包括所有的宽度、高度、背景和背景位置。并将 Command 和 CommandArgument 添加到 LinkBut​​ton。

      【讨论】:

      • 感谢您的帮助。这个解决方案可能有效,但我选择了 nuux 提供的那个,因为它更接近我的想法,不幸的是,我没有太多时间来测试各种答案。所以,这只是对稍后引用这个问题的其他人的通知,这个答案可能工作得很好!
      猜你喜欢
      • 2013-06-02
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多