Friday, May 20, 2005

How to Use Confirm Message Box in Button and DataGrid Events in ASP.NET

1) How to call in Button on Click
 
btnDelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure you want to delete?')== false) return false;")

2) The Way to Call Confirm Message in Grid Events

 
 Write DataGrid code like this in ASPX Page .
 
<asp:datagrid onDeleteCommand="dgrd_DeleteCommand"..............  ......>
<Columns>
    <asp:BoundColumn ........
    <asp:BoundColumn.........
    <asp:TemplateColumn HeaderText="Removal">
         <ItemTemplate>
      <asp:LinkButton id="lbtnDelete" Text="Delete" CommandName="Delete" runat="server"/>
         </ItemTemplate>
    </asp:TemplateColumn>
Since we are not allowed to add the client-side function in the onClick event declaratively, therefore, in the ItemDataBound or the ItemCreated event of the DataGrid, add these lines 

   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
        if( e.Item.FindControl("lbtnDelete") !=null)
        {
            LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
            lbtnDelete.Attributes.Add("onclick","return confirm('The record selected will be deleted permanently. Are you sure you wish to delete this merchant?');");
         }
   }

For the DeleteCommand event,


public void dgrd_DeleteCommand(Object sender, DataGridCommandEventArgs e)
{
     // use the findcontrol to get the value of the textbox
    // and do your deletion operation here
}




Have a Nice Day
 
Ritesh Kumar Kesharwani
Software Professional (MCAD)
Cell  :  011-91-9845657636


Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.

No comments: