【问题标题】:How to populate a GridView column with a button如何使用按钮填充 GridView 列
【发布时间】:2014-08-13 14:34:57
【问题描述】:

我有以下GridView

<asp:GridView ID="gvDispMsg" ClientIDMode="Static" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" EmptyDataText="There is no data to display" runat="server" AllowPaging="false" AllowSorting="true">
    <Columns>
        <asp:BoundField HeaderText="Delete Message" HeaderStyle-Width="12%" />
        <asp:BoundField HeaderText="ID" HeaderStyle-Width="12%" DataField="ID" />
        <asp:BoundField HeaderText="Date" HeaderStyle-Width="13%" DataField="Created" />
        <asp:BoundField HeaderText="Message" HeaderStyle-Width="45%" DataField="Message" />
        <asp:BoundField HeaderText="Active" HeaderStyle-Width="10%" DataField="Active" />
        <asp:BoundField HeaderText="Created By" HeaderStyle-Width="20%" DataField="CreatedBy" />
    </Columns>
</asp:GridView>

它是从在代码隐藏中执行的 SQL 表中填充的。唯一未填充的列是 Delete Message 列。

如何为Delete Message 列中的每一行添加一个按钮,该按钮采用ID 数据字段的参数?

【问题讨论】:

  • 在哪里添加?
  • 而不是删除消息

标签: c# sql asp.net gridview


【解决方案1】:

添加

<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"

并在 CodeBehind 中处理 NewEvent

添加 &lt;asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/&gt; 而不是 BoundField

添加

<asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </ItemTemplate> 
    <EditItemTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </EditItemTemplate>
    <FooterTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </FooterTemplate>                  
</asp:TemplateField>

您最有能力在每个州配置此列

【讨论】:

  • 但是 HeaderText 会丢失吗?
  • 我需要多个删除按钮,或者我可以只有一个吗? ://
  • 你想拥有多少就拥有多少。只需添加多个 ButtonField 或 TemplateFields
  • 所以我应该添加 ? 而不是 BoundField
  • 添加多个 ButtonField 或多个 TemplateFields - 取决于哪种方法更适合您的需求。 TemplateFields 让你更灵活,ButtonField 让代码更易读
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
相关资源
最近更新 更多