我有一台GridView。我的GridView有一个包含"Options“列的列。此列包括传统的CommandField选项(编辑、删除等)。我已经将代码设置为在使用CommandField时工作。但是,我需要进行一些自定义格式化,因此需要将CommandField转换为TemplateField。
我的问题是,如何从TemplateField中的各种LinkButton元素触发OnRowCommand、OnRowEditing、OnRowDeleting和OnRowUpdating事件?
谢谢!
发布于 2009-10-30 11:26:28
您所要做的就是将模板列中LinkButton的CommandName属性设置为“编辑”用于编辑、“删除”用于删除和“更新”用于更新。这将分别触发GridView RowEditing、RowDeleting和RowUpdating事件。若要触发RowCommand事件,需要设置GridView控件的OnRowCommand属性。
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--To fire the OnRowEditing event.-->
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<!--To fire the OnRowDeleting event.-->
<asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
<!--To fire the OnRowUpdating event.-->
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update"
Text="Update">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>发布于 2010-09-08 06:16:50
我也有同样的问题。
对于编辑,我执行了以下操作:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="EditButton"
runat="server"
CommandName="Edit"
Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="UpdateButton"
runat="server"
CommandName="Update"
Text="Update" />
<asp:LinkButton ID="Cancel"
runat="server"
CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>这允许显示/隐藏更新和取消按钮。
至于delete,我使用了以下方法:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DeleteButton"
Text="Delete"
CommandName="Delete"
runat="server" />
</ItemTemplate>
</asp:TemplateField>发布于 2012-04-11 18:55:27
单击属性中的列,添加字段,然后单击"Convert this CommandField(Edit,update,Cancel) to templateField“
切换到源代码并自动添加代码。
https://stackoverflow.com/questions/1647428
复制相似问题