在.net2.0中,怎么样实现对gridview删除行时弹出确认对话框

1:

在GridView的属性对框话框中点击“Columns”进入它的“字段”设计器。接着在“字段”设计器中选择以前已加上的那个 CommandField“删除”列,这时在它的属性列表下会看到一个“将此它段转换为 TemplateFied”的项,点击将它转换为TemplateFied列。

完后退出该字段设计器,切换到源码视图你会发现该列已由原来的:<asp:CommandField ShowDeleteButton="True" />
变为了:
<asp:TemplateField ShowHeader="False">
                                  <ItemTemplate>
                                      <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"    Text="删除"></asp:LinkButton>
</ItemTemplate>

最后在<asp:LinkButton>中加入:OnClientClick="return confirm('确认要删除吗?');"

2:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex > -1)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
LinkButton lbtnDelete = (LinkButton)e.Row.FindControl("lbtnDelete");
if (lbtnDelete != null)
{
lbtnDelete.CommandArgument = id.ToString();
lbtnDelete.Attributes.Add("onClick", "<script>return confirm('是否确认删除!')</script>");
}
}
}

}
3,先引用System.windwos.Forms,然后在进行处理.
using System.Windows.Forms;

protected void gvNewList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DialogResult result = MessageBox.Show("确定要删除本行吗?", "信息提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2,MessageBoxOptions.ServiceNotification);
if (result == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
4,添加一个删除列

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
TableCell tc = (TableCell)e.Row.Cells[e.Row.Cells.Count - 1];
for (int i = 0; i < tc.Controls.Count; i += 2)
{
// cerco il controllo ImageButton (ho utilizzato quello)
Object o = tc.Controls[i];
if (o is ImageButton)
{
// controllo trovato!
// ora aggiungo l'evento js onClick per chiedere conferma all'utente
ImageButton lb = (ImageButton) o;
((ImageButton)lb).Attributes.Add("onclick", @"javascript:return confirm('Attenzione: sicuro di voler cancellare?');");
}
}
}
-------------------------------------------------------------
<asp:TemplateField ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="16px" />
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server" CausesValidation="False" CommandName="Delete" ImageUrl="~/img/ico_elimina.gif" AlternateText="Cancella data" OnClientClick="return confirm('Sicuro di voler cancellare?');" />
</ItemTemplate>
</asp:TemplateField>

以上方法总结

---------Template way-----------------------------------------------

<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick='return confirm("Are you sure you want to delete this record?");'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

-------------RowDeleting method------------------------------------------------

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("<script>window.confirm('确定删除吗?');</script>");
}

-------------RowDataBound method--------------------------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确实要删除该记录吗?')");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值