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

[Solved] Closing the edit template in a rad grid...

2 Answers 326 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 25 Aug 2009, 08:10 PM
I am on Vista, using Firefox 3.5.2, Visual Studio 2008, and Telerik 2009.02.0701.20

I have a Rad Grid that is using a collection of objects in the session as its data source. I find that when I edit the item in the grid on the UI, the edit form (defined in a template) is displayed, but when I click on the Insert / Update button, the form stays visible. Clicking on the Cancel button causes the form to disappear (as expected). What do I need to do to cause the edit form to disappear on Insert / Update?

For a sample, here is my aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DiscountTest.aspx.cs" Inherits="Cleopatra.pages.Admin.DiscountTest" MasterPageFile="../../Main.Master" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="cphBody"
  <!-- custom head section --> 
  <style type="text/css"
    .bigModuleBottom td 
    { 
      padding: 0; 
    } 
  </style> 
  <!-- end of custom head section --> 
  <!-- content start --> 
  <span style="font-weight: bold;">Discounts</span> 
  <br /> 
  <br /> 
  <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"
 
    <script type="text/javascript"
      function RowDblClick(sender, eventArgs) { 
        sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical()); 
      } 
    </script> 
 
  </telerik:RadCodeBlock> 
  <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
    <AjaxSettings> 
      <telerik:AjaxSetting AjaxControlID="RadGrid1"
        <UpdatedControls> 
          <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
        </UpdatedControls> 
      </telerik:AjaxSetting> 
    </AjaxSettings> 
  </telerik:RadAjaxManager> 
  <telerik:RadGrid ID="RadGrid1" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" AllowSorting="True" AutoGenerateColumns="false" AllowPaging="True" 
    GridLines="None" runat="server" ShowFooter="True" PageSize="10" AllowAutomaticInserts="True" 
    OnNeedDataSource="RadGrid1_NeedDataSource" OnInsertCommand="RadGrid1_ItemInserted" 
    OnDeleteCommand="RadGrid1_ItemDeleted" OnUpdateCommand="RadGrid1_ItemUpdated"
    <PagerStyle Mode="NextPrevAndNumeric" /> 
    <MasterTableView Width="400px" CommandItemDisplay="TopAndBottom" DataKeyNames="EntryID"
      <Columns> 
        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton" /> 
        <telerik:GridBoundColumn HeaderText="ID" DataField="EntryID" UniqueName="EntryID" Display="false" Visible="false" /> 
        <telerik:GridBoundColumn HeaderText="Flat Fee?" UniqueName="FlatFee" DataField="FlatFee" /> 
        <telerik:GridBoundColumn HeaderText="Number of Loans" DataField="NumLoans" UniqueName="NumLoans" /> 
        <telerik:GridBoundColumn HeaderText="Discount" DataField="Discount" UniqueName="Discount" /> 
        <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete" ButtonType="ImageButton" /> 
      </Columns> 
      <EditFormSettings EditFormType="Template"
        <FormTemplate> 
          <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" style="border-collapse: collapse; background: white;"
            <tr class="EditFormHeader"
              <td colspan="2" style="font-size: small"
                <b>Discount Details</b> 
              </td> 
            </tr> 
            <tr> 
              <td> 
                <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module"
                  <tr> 
                    <td> 
                      <asp:HiddenField runat="server" ID="hEntryID" Value='<%# Bind("EntryID") %>' /> 
                    </td> 
                    <td> 
                      &nbsp; 
                    </td> 
                  </tr> 
                  <tr> 
                    <td> 
                      <asp:CheckBox runat="server" ID="cbFlatFee" Text="Flat fee?" /> 
                    </td> 
                    <td> 
                      &nbsp; 
                    </td> 
                  </tr> 
                  <tr> 
                    <td> 
                      Number of Loans: 
                    </td> 
                    <td> 
                      <telerik:RadTextBox ID="txtNumLoans" runat="server" MaxLength="50" Text='<%# Bind("NumLoans") %>' /> 
                    </td> 
                  </tr> 
                  <tr> 
                    <td> 
                      Discount: 
                    </td> 
                    <td> 
                      <telerik:RadTextBox ID="txtDiscount" runat="server" MaxLength="50" Text='<%# Bind("Discount") %>' /> 
                    </td> 
                  </tr> 
                </table> 
              </td> 
            </tr> 
            <tr> 
              <td> 
                &nbsp; 
              </td> 
            </tr> 
            <tr> 
              <td align="right" colspan="2"
                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' /> 
                &nbsp; 
                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" /> 
              </td> 
            </tr> 
          </table> 
        </FormTemplate> 
      </EditFormSettings> 
    </MasterTableView> 
    <ClientSettings> 
      <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" /> 
    </ClientSettings> 
  </telerik:RadGrid> 
  <!-- content end --> 
</asp:Content> 
 

And here is the code behind:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
namespace Cleopatra.pages.Admin 
  public partial class DiscountTest : System.Web.UI.Page 
  { 
    private class DiscountEntry 
    { 
      private int _EntryID = -1; 
      public int EntryID 
      { 
        get { return _EntryID; } 
        set { _EntryID = value; } 
      } 
 
      private bool _DiscountIsRate = false;       
      public bool DiscountIsRate 
      { 
        get { return _DiscountIsRate; } 
        set { _DiscountIsRate = value; } 
      } 
 
      private int _NumLoans = 0;       
      public int NumLoans 
      { 
        get { return _NumLoans; } 
        set { _NumLoans = value; } 
      } 
 
      private double _Discount = 0.0;       
      public double Discount 
      { 
        get { return _Discount; } 
        set { _Discount = value; } 
      } 
    } 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
      object obj = Session["discounts"]; 
      List<DiscountEntry> discounts = (List<DiscountEntry>) Session["discounts"]; 
      if (discounts == null
      { 
        discounts = new List<DiscountEntry>(); 
        Session.Add("discounts", discounts); 
      } 
    } 
 
    protected void RadGrid1_PreRender(object sender, System.EventArgs e) 
    { 
      if (!this.IsPostBack) 
      { 
        RadGrid1.EditIndexes.Add(1); 
        RadGrid1.Rebind(); 
        RadGrid1.MasterTableView.Rebind(); 
      } 
    } 
 
 
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
      List<DiscountEntry> discounts = (List<DiscountEntry>) Session["discounts"]; 
      RadGrid1.DataSource = discounts; 
    } 
 
    protected void RadGrid1_ItemUpdated (object source, GridCommandEventArgs e) 
    { 
      if (e.Item != null
      { 
        // Get the form controls out 
        GridEditFormItem editFormItem = (GridEditFormItem) e.Item; 
        HiddenField hEntryID = (HiddenField) editFormItem.FindControl("hEntryID"); 
        CheckBox cbFlatFee = (CheckBox) editFormItem.FindControl("cbFlatFee"); 
        RadTextBox txtNumLoans = (RadTextBox) editFormItem.FindControl("txtNumLoans"); 
        RadTextBox txtDiscount = (RadTextBox) editFormItem.FindControl("txtDiscount"); 
        int entryID = Convert.ToInt32(hEntryID.Value); 
        // Locate and update the discount entries 
        List<DiscountEntry> discounts = (List<DiscountEntry>) Session["discounts"]; 
        foreach (DiscountEntry entry in discounts) 
        { 
          if (entry.EntryID == entryID) 
          { 
            entry.DiscountIsRate = !cbFlatFee.Checked; 
            entry.NumLoans = Convert.ToInt32(txtNumLoans.Text); 
            entry.Discount = Convert.ToDouble(txtDiscount.Text); 
            break
          } 
        } 
      } // valid edit item 
      RadGrid1.Rebind(); 
      RadGrid1.MasterTableView.Rebind(); 
    } 
 
    protected void RadGrid1_ItemDeleted(object source, GridCommandEventArgs e) 
    { 
      if (e.Item != null
      { 
        List<DiscountEntry> discounts = (List<DiscountEntry>) Session["discounts"]; 
        discounts.RemoveAt(e.Item.ItemIndex); 
      } // valid delete item 
    } 
 
    protected void RadGrid1_ItemInserted(object source, GridCommandEventArgs e) 
    { 
      if ((e.Item is GridEditFormInsertItem) && (e.Item.OwnerTableView.IsItemInserted)) 
      { 
        // Get the user controls in the form 
        GridEditFormItem editFormItem = e.Item as GridEditFormItem; 
        HiddenField hEntryID = (HiddenField) editFormItem.FindControl("hEntryID"); 
        CheckBox cbFlatFee = (CheckBox) editFormItem.FindControl("cbFlatFee"); 
        RadTextBox txtNumLoans = (RadTextBox) editFormItem.FindControl("txtNumLoans"); 
        RadTextBox txtDiscount = (RadTextBox) editFormItem.FindControl("txtDiscount"); 
 
        // Create a new discount table entry and add it to the loan type 
        List<DiscountEntry> discounts = (List<DiscountEntry>) Session["discounts"]; 
        // Determine next available ID 
        int newEntryID = 0; 
        Dictionary<int,bool> discountHash = new Dictionary<int,bool>(); 
        foreach (DiscountEntry discount in discounts) 
        { 
          discountHash.Add(discount.EntryID, true); 
        } 
        int i = 0; 
        while (true
        { 
          if (!discountHash.ContainsKey(++i)) 
          { 
            newEntryID = i; 
            break
          } 
        } // while loop 
        DiscountEntry entry = new DiscountEntry(); 
        entry.EntryID = newEntryID; 
        entry.DiscountIsRate = !cbFlatFee.Checked; 
        entry.NumLoans = Convert.ToInt32(txtNumLoans.Text); 
        entry.Discount = Convert.ToDouble(txtDiscount.Text); 
        discounts.Add(entry); 
      } // valid insertion 
      RadGrid1.Rebind(); 
      RadGrid1.MasterTableView.Rebind(); 
    } 
  
  
  } 
 
 

Any thoughts and ideas for me?

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Aug 2009, 04:34 AM
Hello

It seems that you are performing manual insert and update operations. Hence, you should not be setting the AllowAutomaticInserts/AllowAutomaticUpdates to true which is why the forms do not close on hitting the update or insert buttons. Also the automatic operations work only when binding the grid to a declarative data source using the DataSourceID property of the grid. So you need to set the AllowAutomaticInserts/AllowAutomaticUpdates properties to false and also make use of the Insert/Update Command events to handle the manual insert and update operations rather than using ItemInserted/ItemUpdated events of the grid.

Thanks
Princy.
0
Dan
Top achievements
Rank 1
answered on 26 Aug 2009, 07:02 PM
That was the solution. Thanks, Princy!
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Share this question
or