嘗試這個
HTML標記
下面是APS.Net GridView控件的HTML標記。這裏我使用CommandField和OnRowDeleting事件來刪除GridView Row。因此,我會將JavaScript確認框應用到CommandFieldDelete按鈕本身。
<asp:GridView ID="GridView1" CssClass = "Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
運用JavaScript的確認框到GridView CommandField中 刪除按鈕
要應用的JavaScript確認框,我正在尋找,因爲在GridView細胞指數2的控制按鈕它有CommandField。一旦
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
}
}
}
}
使用CommandField中和OnRowDeleting 事件
下面是代碼的ASP.Net的GridView行使用OnRowDeleting事件刪除刪除ASP.Net的GridView行
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
BindGrid();
}
你必須做出動態的數據表格來存儲gridview中的值,這是否太需要一些代碼來解釋... –
http:// dotprogramming .blogspot.com/2013/10/how-to-delete-multiple-rows-from.html –
http://www.codeproject.com/Questions/267426/how-to-delete-the-gridview-row-with -templatefield –