【问题标题】:Converting an asp:ButtonField to an asp:TemplateField in a GridView Control将 GridView 控件中的 asp:ButtonField 转换为 asp:TemplateField
【发布时间】:2008-12-03 20:22:25
【问题描述】:

我目前有一个带有 asp:ButtonField 作为列之一的 gridview。命令的事件处理程序从命令参数中提取 gridview 的行 ID,并使用它来执行一些逻辑。我现在需要切换到使用此列的模板字段,并且想做这样的事情:

<asp:TemplateField HeaderText="Action">
    <ItemStyle HorizontalAlign="Center" />
        <ItemTemplate>
            <asp:LinkButton CommandName="myaction" CommandArgument="<%#Eval("id")%>" Text="do action" runat="server"/>
        </ItemTemplate>
 </asp:TemplateField>

我的问题出在 CommandArgument 属性上——我不知道如何让它成为 GridView 中的行 ID。 Eval("id") 不起作用 - 有谁知道行 id 属性的名称是什么?或者有更好的方法来做到这一点?

【问题讨论】:

    标签: asp.net gridview linkbutton templatefield


    【解决方案1】:

    在 Microsoft 网站上查看此文档。

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

    您不需要绑定行 ID。命令参数用于事件参数,而不是行参数......如果有意义的话。

    我想我想说的是行 ID 是隐式的。当您访问事件 CommandArgs 时,它应该针对您尝试引发的事件的特定内容。

    即如果你想分页,你会在 CommandArgument 中有 "next"、"prev"、"first"、"last" 或页码。

    假设您有一个“删除”按钮或图像按钮。它被用户点击;在回发时收到删除事件,并从行向上冒泡到网格,整个行对象作为参数传递。然后网格会删除该行,就好像它是控件集合中的单个控件一样 - 因为它就是这样,表格是表格行的集合。

    【讨论】:

      【解决方案2】:
      CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"
      

      【讨论】:

        猜你喜欢
        • 2021-01-23
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 2017-05-02
        • 2016-05-11
        相关资源
        最近更新 更多