I want to delete record from GridView.Before to this ask for confirmation like "Are you sure to delete?"
I used command field in GridView,
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
I wrote a function in 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;
}
}
How I will call this on delete click. Kindly suggest !
I want to delete record from GridView.Before to this ask for confirmation like "Are you sure to delete?"
I used command field in GridView,
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
I wrote a function in 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;
}
}
How I will call this on delete click. Kindly suggest !
Share Improve this question edited Apr 5, 2011 at 14:13 Muhammad Akhtar 52.2k37 gold badges139 silver badges191 bronze badges asked Apr 1, 2011 at 2:07 im uselessim useless 7,4517 gold badges39 silver badges49 bronze badges4 Answers
Reset to default 5You can't achieve this using the command field, you have to make a template field:
<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>
It will behave the same way you are doing currently with the Command Field.
I would do the same as @Muhammad told you, and at the server side code for deleting I would also register an script for showing the "Record Deleted" message, as follows;
public void MethodForDeletingARecord()
{
ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "RecordDeletedMessage", "javascript:alert('Record Deleted');", true);
}
I think you can achieve this for commandfield
Assuming it would be the first column, 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?');"
}
}
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();");
}
}