ASP.NET - radgrid OnitemCommand event for deleting a record

Asked By Arun Kumar on 11-Jul-11 06:00 AM
Hi,

I have written java script for delete confirmation:

<script type="text/javascript">
        function deleteTemplate() {
            var agree = confirm("Are you sure you wish to continue?");
            if (agree) {
                return true;
            }
            else {
                return false;
            }
        }

Now when i click on Yes, it should go to server side and execute the radgrid OnItemCommand event.how do i do that?Also,please tell me how to use this event for deleting a record.

Kindly let me know if you need more information.

Many Thanks.



Ravi S replied to Arun Kumar on 11-Jul-11 06:03 AM
Hi

You can do this using DataKekNames/DataKeyValues. Here is an example:
...
<MasterTableView DataKeyNames="ID" AutoGenerateColumns="false" GroupLoadMode="Client" Width="475px">
...

if (e.CommandName == "Delete")
{

    object ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"];

}

Ravi S replied to Arun Kumar on 11-Jul-11 06:04 AM
HI

here are links with examples

refer these links

http://www.telerik.com/community/forums/aspnet/grid/how-to-get-custom-id-object-in-onitemcommand-event.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-onitemcommand-server-event-is-not-fired-when-i-have-a-oncommand-client-event-on-the-radgrid.aspx
Jitendra Faye replied to Arun Kumar on 11-Jul-11 06:04 AM

you can display confirmation window from client side only-

For that put one Button in ItemTemplate of RadGrid.

Use this code-

<ItemTemplate>
<asp:Button ID="btnDelate" runat="server" onclick="btnDelete_Click" OnClientClick="return confirm('Are you certain you want to delete this product?');"

Text="Delete" />

</ItemTemplate>

Using this you can disply confirmation window.

Use this and let me know.

Reena Jain replied to Arun Kumar on 11-Jul-11 06:05 AM
Hi,

You can use,

a)  hiddenfield to store the value of the Confirm function call
b) or a script that break the flow like the one below:

Step One:
On the client-side create a Javascript function that would show the confirm message, and based on the input would call the server side button click.

<script type="text/javascript" language="javascript">
function GetInputFromUser()
{
   if(confirm("Are you sure you want to delete this?")== true)
   {
     //call the server side button click , from where you can trigger the actual sql code.
     document.getElementById("btnDoSQL").click();
   }
}
</script>

Step Two:
From the server-side, invoke the javascript function on a button click or anywhere, you need to break the flow.
protected void btnAskUser_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myConfirm", "GetInputFromUser();", true);
}

Step Three:
The ServerSide function that will be invoked on confirm : yes.
protected void gridevent()
{
//this code will be invoked from the client side script : GetInputFromUser.
//and only if he pressed 'Yes' ..i.e value == true.
}

Hope this will help you
Arun Kumar replied to Jitendra Faye on 11-Jul-11 06:08 AM
Vickey,

Thanks it works.can you please tell me how to use ItemCommand event for deleting records?

Thanks.
Arun Kumar replied to Reena Jain on 11-Jul-11 06:20 AM
actually i have a stored procedure for deleting the record based on the ID.Currently i have written :

 <telerik:GridTemplateColumn HeaderText="Delete" HeaderStyle-Width="60px">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnDeleteTemplate" CommandName="Remove" OnClientClick="return confirm('Are you sure you wish to continue?');"
                                runat="server">

So i think i need to access the SP in the Item Command event right?how do i do that?

dipa ahuja replied to Arun Kumar on 11-Jul-11 06:29 AM
Add the confirmation in the dataBound event:

 <rad:GridButtonColumn CommandName="Delete" Text="Delete"
    
UniqueName= "DeleteColumn"></rad:GridButtonColumn>

private void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
{
  
if(e.Item is GridDataItem)
  {
   GridDataItem dataItem = e.Item
 as GridDataItem;
   
string contactName = dataItem["ContactName"].Text;

   LinkButton button = dataItem[
"DeleteColumn"].Controls[0] as LinkButton;
   button.Attributes[
 "onclick"] = "return confirm('Are you sure you want to delete " +
   contactName +
 "?')";
  }
}
Then implement the itemCommand event :
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    GridDataItem item = (GridDataItem)e.Item;
    string Manuf_ID = item["empid"].Text;
    //delete code
  }
}
Jitendra Faye replied to Arun Kumar on 11-Jul-11 06:40 AM
For deleting record using ItemCommand in RadGrid Use these steps-

Step1: Take one button inside RadGrid ItemTemplate Like this-

<ItemTemplate>
<asp:Button ID="btnDelate" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you certain you want to delete this product?');"

Text="Delete" />

</ItemTemplate>

Step 2: Now implement ItemDeleted() event of RadGrid.


protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
      {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("ProductID").ToString();
      
      //Now wriet delete code based on id
        //After Delete again Bind record to RadGrid.

      }



Try this code and let me know.


Arun Kumar replied to Jitendra Faye on 11-Jul-11 06:52 AM
i have called the Stored Procedure for deleting records in the DAL and then linked to BLL and UI layers:

public bool DeleteTemplateByIdUI(int TemplateId)//UI function
        {
            return new SHTemplateBA().DeleteTemplateByIdBA(TemplateId);
}
now how do i call the function DeleteTemplateByIdUI(int TemplateId) in the ItemCommandevent in order to delete a record?any idea?
Jitendra Faye replied to Arun Kumar on 11-Jul-11 07:14 AM
Ok, then just call DeleteTemplateByIdUI() in ItemDeleted() event.


protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
      GridDataItem dataItem = (GridDataItem)e.Item;
      Int id = convert.ToInt32( dataItem.GetDataKeyValue("ProductID").ToString());
    
      //Declare objcet of your BAL Class and call this function DeleteTemplateByIdUI()
  
  
    // Like this -
     
     bool flag=  NameofClassObjcet.DeleteTemplateByIdUI(id);

    }

Try this code and let me know.

Reena Jain replied to Arun Kumar on 11-Jul-11 07:29 AM
Hi,

On ItemCommand method of radgrid use the following command to delete the record

if (e.CommandName == "Remove")
  {
   //check hidden field value
   if(hdn1.Value==1)
   {
     //delete code
   }
    
  }

Hope this will help you
Arun Kumar replied to Reena Jain on 11-Jul-11 08:06 AM
Hi ,

can u give me an example to store the value using hiddenfield?