我有以下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
数据字段的参数?
答案 0 :(得分:1)
添加
<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"
并在CodeBehind中处理NewEvent
或
添加<asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/>
而不是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>
您可以在每个州配置此列的能力最强