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

InsertCommand, UpdateCommand, and Deletecommand not firing

1 Answer 257 Views
Grid
This is a migrated thread and some comments may be shown as answers.
chris
Top achievements
Rank 1
chris asked on 01 Mar 2012, 07:35 PM
Hello everyone,

I have a telerik Grid and when I try to use insert, update or delete the events on in the code behind never fire. The popup does go away though and it looks like the grid is being updated. This is what I have for the code:

<telerik:RadGrid
            ID="ClaimFormPartsGrid" runat="server"
            GridLines="None" Skin="Simple"
            AutoGenerateColumns="False"
            AllowSorting="False"
            AllowMultiRowSelection="False">
            <MasterTableView
                EditMode="PopUp" CommandItemDisplay="Top"
                InsertItemDisplay="Top" CommandItemSettings-ShowRefreshButton="False"
                CommandItemSettings-ShowAddNewRecordButton="True" AutoGenerateColumns="False" EnableViewState="True"
                DataKeyNames="key">
                <Columns>
                    <telerik:GridEditCommandColumn UniqueName="EditCommand"/>
                    <telerik:GridBoundColumn UniqueName="key" DataField="key" HeaderText="Name"/>
                    <telerik:GridBoundColumn UniqueName="value" DataField="value" HeaderText="Value"/>
                    <telerik:GridButtonColumn UniqueName="DeleteCommand" Text="Delete" CommandName="DeleteCommand" />
                </Columns>
                <EditFormSettings EditFormType="Template" InsertCaption="Adding new Claim Form Part"
                    CaptionFormatString="Editing Claim Form Part" PopUpSettings-Modal="True" PopUpSettings-ScrollBars="None">
                    <FormTemplate>
                        <div class="rust-form">
                            <fieldset class="no-bg">
                                <legend></legend>
                                <ul>
                                    <li>
                                        <asp:Label runat="server" ID="keyNameLabel" AssociatedControlID="KeyNameTextBox">Name: </asp:Label>
                                        <asp:TextBox runat="server" ID="keyNameTextBox"></asp:TextBox>
                                    </li>
                                    <li>
                                        <asp:Label runat="server" ID="keyValueLabel" AssociatedControlID="KeyValueTextBox">Value: </asp:Label>
                                        <asp:TextBox runat="server" ID="keyValueTextBox"></asp:TextBox>
                                    </li>
                                </ul>
                            </fieldset>
                            <ul class="rust-action-btns">
                                <li>
                                    <asp:Button ID="InsertUpdateButton" runat="server"
                                                Text="Save" CssClass="rust-primary-btn" TabIndex="1"
                                                CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "" %>' /> />
                                </li>
                                <li>
                                    <asp:Button ID="ClaimCancelButton" runat="server" CausesValidation="False"
                                                Text="Cancel" CssClass="rust-secondary-btn"
                                                CommandName="Cancel" />
                                </li>
                            </ul>
                        </div>
                    </FormTemplate>
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.ClaimFormPartsGrid.NeedDataSource += this.ClaimFormPartsGrid_NeedData;
    this.ClaimFormPartsGrid.ItemDataBound += this.ClaimFormPartsGrid_ItemDataBound;
    this.ClaimFormPartsGrid.InsertCommand += this.ClaimFormPartsGrid_InsertCommand;
    this.ClaimFormPartsGrid.UpdateCommand += this.ClaimFormPartsGrid_UpdateCommand;
    this.ClaimFormPartsGrid.DeleteCommand += this.ClaimFormPartsGrid_DeleteCommand;
}
 
protected void ClaimFormPartsGrid_NeedData(object sender, GridNeedDataSourceEventArgs e)
{
    var source = new List<KeyValuePair<string, string>>();
    source.Add(new KeyValuePair<string, string>("1", "1"));
    source.Add(new KeyValuePair<string, string>("2", "2"));
    source.Add(new KeyValuePair<string, string>("3", "3"));
    source.Add(new KeyValuePair<string, string>("4", "4"));
    this.ClaimFormPartsGrid.DataSource = source;
}
 
protected void ClaimFormPartsGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if(e.Item.IsInEditMode)
    {
        var gei = e.Item as GridEditableItem;
        if(gei == null)
        {
            return;
        }
 
        var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
        var valueTextbox = (TextBox) gei.FindControl("keyValueTextBox");
 
        if(gei.OwnerTableView.IsItemInserted)
        {
            // Inserting.
            nameTextbox.Text = string.Empty;
            valueTextbox.Text = string.Empty;
        }
        else
        {
            // Editing.
            var keyValue= (KeyValuePair<string, string>) gei.DataItem;
            nameTextbox.Text = keyValue.Key.ToString();
            valueTextbox.Text = keyValue.Value.ToString();
        }
    }
    else
    {
        // displaying
        var gdi = e.Item as GridDataItem;
        if (gdi == null)
        {
            return;
        }
 
        var keyValue= (KeyValuePair<string,string>)gdi.DataItem;
 
 
        var deleteButton = gdi["DeleteCommand"].Controls[0] as LinkButton;
        if(deleteButton != null)
        {
            deleteButton.Attributes["onclick"] = "return confirm('Are you sure you want to delete " + keyValue.Key + "?')";
        }
    }
}
 
protected void ClaimFormPartsGrid_InsertCommand(object sender, GridCommandEventArgs e)
{
    var gei = e.Item as GridEditableItem;
    if (gei == null)
    {
        return;
    }
 
    var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
    var valueTextbox = (TextBox)gei.FindControl("keyValueTextBox");
 
}
 
 
protected void ClaimFormPartsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
{
    var gei = e.Item as GridEditableItem;
    if (gei == null)
    {
        return;
    }
 
    var oldName = (KeyValuePair<string, string>)gei.DataItem;
    var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
    var valueTextbox = (TextBox)gei.FindControl("keyValueTextBox");
 
}
 
protected void ClaimFormPartsGrid_DeleteCommand(object sender, GridCommandEventArgs e)
{
    var gei = e.Item as GridEditableItem;
    if (gei == null)
    {
        return;
    }
 
}


Does anyone have an Idea?

1 Answer, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Mar 2012, 10:02 AM
Hello chris,

<EditFormSettings EditFormType="Template" InsertCaption="Adding new Claim Form Part"
                   CaptionFormatString="Editing Claim Form Part" PopUpSettings-Modal="True" PopUpSettings-ScrollBars="None">
                   <FormTemplate>
                       <div class="rust-form">
                           <fieldset class="no-bg">
                               <legend></legend>
                               <ul>
                                   <li>
                                       <asp:Label runat="server" ID="keyNameLabel" AssociatedControlID="KeyNameTextBox">Name: </asp:Label>
                                       <asp:TextBox runat="server" ID="keyNameTextBox"></asp:TextBox>
                                   </li>
                                   <li>
                                       <asp:Label runat="server" ID="keyValueLabel" AssociatedControlID="KeyValueTextBox">Value: </asp:Label>
                                       <asp:TextBox runat="server" ID="keyValueTextBox"></asp:TextBox>
                                   </li>
                               </ul>
                           </fieldset>
                           <ul class="rust-action-btns">
                               <li>
                                  <asp:Button ID="InsertUpdateButton" Text='<%# (Container is GridEditFormInsertItem) ? "Insert-Save" : "Update-Save" %>'
                                   runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                               </asp:Button>
                                   </li>
                               <li>
                                   <asp:Button ID="ClaimCancelButton" runat="server" CausesValidation="False" Text="Cancel"
                                       CssClass="rust-secondary-btn" CommandName="Cancel" />
                               </li>
                           </ul>
                       </div>
                   </FormTemplate>
               </EditFormSettings>

protected override void OnInit(EventArgs e)
   {
       base.OnInit(e);
       this.ClaimFormPartsGrid.NeedDataSource += this.ClaimFormPartsGrid_NeedData;
       this.ClaimFormPartsGrid.ItemDataBound += this.ClaimFormPartsGrid_ItemDataBound;
       this.ClaimFormPartsGrid.InsertCommand += this.ClaimFormPartsGrid_InsertCommand;
       this.ClaimFormPartsGrid.UpdateCommand += this.ClaimFormPartsGrid_UpdateCommand;
       this.ClaimFormPartsGrid.DeleteCommand += this.ClaimFormPartsGrid_DeleteCommand;
   }
 
   protected void ClaimFormPartsGrid_NeedData(object sender, GridNeedDataSourceEventArgs e)
   {
       var source = new List<KeyValuePair<string, string>>();
       source.Add(new KeyValuePair<string, string>("1", "1"));
       source.Add(new KeyValuePair<string, string>("2", "2"));
       source.Add(new KeyValuePair<string, string>("3", "3"));
       source.Add(new KeyValuePair<string, string>("4", "4"));
       this.ClaimFormPartsGrid.DataSource = source;
   }
 
   protected void ClaimFormPartsGrid_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item.IsInEditMode)
       {
           var gei = e.Item as GridEditableItem;
           if (gei == null)
           {
               return;
           }
 
           var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
           var valueTextbox = (TextBox)gei.FindControl("keyValueTextBox");
 
           if (gei.OwnerTableView.IsItemInserted)
           {
               // Inserting.
               nameTextbox.Text = string.Empty;
               valueTextbox.Text = string.Empty;
           }
           else
           {
               // Editing.
               var keyValue = (KeyValuePair<string, string>)gei.DataItem;
               nameTextbox.Text = keyValue.Key.ToString();
               valueTextbox.Text = keyValue.Value.ToString();
           }
       }
       else
       {
           // displaying
           var gdi = e.Item as GridDataItem;
           if (gdi == null)
           {
               return;
           }
 
           var keyValue = (KeyValuePair<string, string>)gdi.DataItem;
 
 
           var deleteButton = gdi["DeleteCommand"].Controls[0] as LinkButton;
           if (deleteButton != null)
           {
               deleteButton.Attributes["onclick"] = "return confirm('Are you sure you want to delete " + keyValue.Key + "?')";
           }
       }
   }
 
   protected void ClaimFormPartsGrid_InsertCommand(object sender, GridCommandEventArgs e)
   {
       var gei = e.Item as GridEditableItem;
       if (gei == null)
       {
           return;
       }
 
       var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
       var valueTextbox = (TextBox)gei.FindControl("keyValueTextBox");
 
   }
 
 
   protected void ClaimFormPartsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       var gei = e.Item as GridEditableItem;
       if (gei == null)
       {
           return;
       }
 
       // gei.DataItem -- you are not able to get this here -- this line only work in ItemDataBound event.
 
       // you can get DataKey and find this row in dictionary
       string strKey = gei.GetDataKeyValue("key").ToString();
 
       var oldName = (KeyValuePair<string, string>)gei.DataItem;
       var nameTextbox = (TextBox)gei.FindControl("keyNameTextBox");
       var valueTextbox = (TextBox)gei.FindControl("keyValueTextBox");
 
   }
 
   protected void ClaimFormPartsGrid_DeleteCommand(object sender, GridCommandEventArgs e)
   {
       var gei = e.Item as GridEditableItem;
       if (gei == null)
       {
           return;
       }
 
   }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
chris
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or