我想从GridView中删除记录。在此之前要求确认,例如“你确定要删除吗?”
我在GridView中使用了命令字段,
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
我在javascript中写了一个函数
function confirm_Delete()
{
var r = confirm("Are you sure you want to Remove this Record!");
if (r == true)
{
alert("Record Deleted");
return true;
}
else
{
return false;
}
}
如何在删除点击时调用此方法。 请建议!
答案 0 :(得分:5)
使用命令字段无法实现此目的,您必须创建模板字段:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="javascript:return confirm('Are you sure you want to Remove this Record!');">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
它的行为方式与您目前使用命令字段的方式相同。
答案 1 :(得分:5)
我会做相同@Muhammad告诉你,并且在用于删除我还注册一个脚本示出“记录中删除”消息的服务器端代码,如下所示;
public void MethodForDeletingARecord()
{
ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "RecordDeletedMessage", "javascript:alert('Record Deleted');", true);
}
答案 2 :(得分:4)
我认为你可以为commandfield实现这个目标
假设它是第一列,Found Here
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];
db.OnClientClick = "javascript:return confirm('Are you certain you want to delete?');"
}
}
答案 3 :(得分:3)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return Conformation();");
}
}