This is a migrated thread and some comments may be shown as answers.

Can't execute CancelConfirmed in RadGrid ItemCommand event if Grid has No Rows.

4 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 19 Feb 2014, 06:32 PM
Folks
using RadControls for ASP.NET AJAX Q3 2013 with VStudio 10. I am using below link as Prototype.

http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

My Radgrid has Filter enabled.  Below are my steps to reproduce the error:

1) Filtered the Grid with some condition; Grid has no Rows.
2) Clicked Add New Command Item link Button.
3) Now can't Cancel Insert operation from Form Template.  Edit Form Stays open.
4) But if Grid has some rows, Cancel Insert operation works perfectly.

Below is my Code. Any help will be appreciated.

Thanks

Gc_0620

_________

01.protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
02.   {
03.       if (true && e.Item.OwnerTableView.Name == "MainClient") //validated
04.       {
05.           switch (e.CommandName)
06.           {
07.               case "Cancel":
08.                    
09.                   if (HiddenField1.Value == "false")
10.                   {
11.                       e.Canceled = true;
12.                       Session["savedIndex"] = e.Item.ItemIndex;
13.                       RadWindowManager1.RadConfirm("Continue with Cancel? Warning, you will loose any unsaved work!!!",
14.                           "confirmCancelBackFnMain", 350, 150, null, "Cancel Confirm");
15.                   }
16.                    
17.                   break;
18.               case "CancelConfirmed":
19.                   foreach (GridEditableItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
20.                   {
21.                       if (item.IsInEditMode && item.ItemIndex == (int)Session["savedIndex"])
22.                       {
23.                           item.FireCommandEvent("Cancel", String.Empty);
24.                       }
25.                   }
26.                   HiddenField1.Value = "false";
27.                   break;
28.           }
29.       }
30.   }
31.        
32.   __________
33.    
34.   function confirmCancelBackFnMain(arg)
35.   {
36.       var grid = $find("<%= RadGrid1.ClientID %>");
37.       if (arg)
38.       {
39.           //alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
40.           document.getElementById(
41.           '<%=HiddenField1.ClientID
42.           %>').value = "true";
43.           grid.get_masterTableView().fireCommand("CancelConfirmed", String.Empty);
44.       }
45.       else
46.       {
47.           document.getElementById(
48.           '<%=HiddenField1.ClientID
49.           %>').value = "false";
50.       }
51. 
52.       document.getElementById(
53.       '<%=HiddenField1.ClientID
54.       %>').value = "false";
55.   }

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 Feb 2014, 05:49 AM
Hi,

I guess you want to show a Confirm on the Cancel button. Please take a look at the following code snippet.

ASPX:
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="CancelInsert" OnClientClick="radconfirm('The selected application(s) will be deleted.\n\nClick OK to confirm.', callBackFn, null, null, '', 'Cancel');return false;">
</asp:Button>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "CancelInsert")
  {
    e.Canceled = true;
  }   
}

JS:
<script type="text/javascript">
  function callBackFn(confirmed) {
    if (confirmed) {
      $find('<%= RadGrid1.ClientID %>').get_masterTableView().fireCommand("CancelInsert");
      }
    }
</script>

Thanks,
Princy
0
gc_0620
Top achievements
Rank 1
answered on 20 Feb 2014, 03:02 PM
Thanks so much Princy. As always your suggestion is sincerely appreciated..
0
gc_0620
Top achievements
Rank 1
answered on 19 May 2014, 08:34 PM
Hi all,

How do you implement the same solution for Child Table? This above solution works perfectly for Master Table but unfortunately it is not working for Child Table. Cancel Insert operation from Form Template Edit Form Stays open for Child Table.
Thanks
gc_0620
0
Princy
Top achievements
Rank 2
answered on 20 May 2014, 06:43 AM
Hi,

You can set the same Cancel button for all the InsertForm, and set the CommandName to something else other than "CancelInsert".

ASPX:
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="CancelInsertForm" OnClientClick="radconfirm('The selected application(s) will be deleted.\n\nClick OK to confirm.', callBackFn, null, null, '', 'Cancel');return false;">
</asp:Button>

JS:
<script type="text/javascript">
  function callBackFn(confirmed) {
   if (confirmed) {
     $find('<%= RadGrid1.ClientID %>').get_masterTableView().fireCommand("CancelInsertForm");
    }
  }
</script>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "CancelInsertForm")
  {
    e.Canceled = true;
    e.Item.OwnerTableView.IsItemInserted = false;         
    e.Item.OwnerTableView.Rebind();
  }
}

Thanks,
Princy
Tags
Grid
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
gc_0620
Top achievements
Rank 1
Share this question
or