【发布时间】:2014-11-24 16:11:02
【问题描述】:
我想在单击 ImageButton 时从 Gridview 中删除记录。请参阅 GridView 代码供您参考:-
<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3" AutoGenerateColumns="false" OnDataBound="grdCSRPageData_DataBound" PageSize="5" AllowPaging="true" OnPageIndexChanging="grdCSRPageData_PageIndexChanging" OnRowCommand="grdCSRPageData_RowCommand">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField ItemStyle-Width="30">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" />
<asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" />
<asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" />
<asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/images/edit.png" Width="15" Height="15" />
<span onclick="return confirm('Are you sure want to delete?')">
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="DeleteRow" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
尝试使用 GridView 的“RowCommand”属性并卡住如何删除行 见代码:-
protected void grdCSRPageData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteRow")
{
//incase you need the row index
int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
int Id = Convert.ToInt32(e.CommandArgument);
//followed by your code
}
}
【问题讨论】:
-
祝你好运!看来你有一个好的开始。你真的有问题吗?
-
@entropic: 我需要更新和删除 ImageButton 的代码
-
...到目前为止,您尝试了哪些不起作用的方法?
-
@entropic:我尝试了带有 CommandName 属性的 onRowCommand,但它不起作用。 ' protected void grdCSRPageData_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DeleteRow") { //如果你需要行索引 int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer) .RowIndex; int Id = Convert.ToInt32(e.CommandArgument); //后面跟着你的代码 } }' 在我的代码中写什么从表格和网格视图中删除
-
1.您应该编辑您的问题并将该代码放在那里。 2.“不工作”是什么意思? 3. 我将在这里暗中尝试 - 但您实际上并没有定义可以在上面代码中的
ImageButton上引用的命令名称。