先介紹最簡單的方法
1.利用OnClientClick事件
<ItemTemplate>
<asp:ImageButton ID="IbnDel" runat="server" ToolTip="刪除" CommandName="Delete" ImageUrl="/Images/delete01.gif" OnClientClick="javascript:return confirm('你確定要刪除嗎?將不可恢複哦');" />
</ItemTemplate>
2.後台代碼裡寫,算最簡便的一種方法了
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[8].Attributes.Add("onclick", "return confirm('刪除此條記錄?');");
}
}
3.遍曆每個元素,找到你要的控制項,添加刪除確認.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
LinkButton l;
for(int i=0;i<e.Row.Cells.Count;i++)
{
for (int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
{
if (e.Row.Cells[i].Controls[j].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
l = (LinkButton)e.Row.Cells[i].Controls[j];
if (l.Text == "刪除")
{
l.Attributes.Add("onclick", "return confirm('確定要刪除這個分類嗎?');");
}
}
}
}
}
4.最後附上DataGrid的刪除確認方法
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)//添加刪除提示
{
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
case ListItemType.Item:
{
LinkButton lbtDelete = (LinkButton)e.Item.Cells[2].Controls[0];
lbtDelete.Attributes.Add("onclick","return confirm('您確定要刪除[" + e.Item.Cells[0].Text + "]的資料嗎?此操作將刪除[" + e.Item.Cells[0].Text + "]的所有小類以及所有新聞!');");
break;
}
}
}