Monday, May 09, 2005

Confirmation when deleting a record in the datagrid.

There are 2 possible ways to handle this scenario..


First solution is that, there are checkboxes in each row and on selecting the row, and click the delete button the records are deleted.


In Page load of the code-behind file,
btnDelete.Attributes.Add ("onClick", "return validate ('delete') ;"); // btndelete is command button


Add a client side method in UI,
function validate (Action)
{
if (! checkbox selected)
alert (?select any checkbox?); return false;
else
{
if (confirm("Are you sure you want to " + Action + " the selected User(s)?"))
return true;
else
return false;
}
}


And second solution is that, there are buttons in each row and on click the delete button the records are deleted.


In Page load of the code-behind file,
Call the Method, AddAttribute();


private void AddAttribute()
{
foreach( DataGridItem di in Datagrid1.Items)
{
LinkButton lnkBtn = (LinkButton)di.FindControl("lnkBtnDelete") ;
cmdBtn.Attributes.Add("onClick", "return confirm('Are you sure, you wish to delete this record?');");
}
}


In UI part,
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelete" Runat="server" CausesValidation="false" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

Happy Programming,

Kannan


Comments: Post a Comment

This page is powered by Blogger. Isn't yours?