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

RadGrid event does not fire

7 Answers 701 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yoong
Top achievements
Rank 1
Yoong asked on 01 Aug 2013, 02:18 AM
Hello,

I have a hierarchical RadGrid with a MasterTableView and DetailTables. In order to only allow only one EditForm form visible at a time, I have added the following code to clear any outstanding add or edit form.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
 
        //Clear and reset all outstanding edit form
        e.Item.OwnerTableView.IsItemInserted = false;
        e.Item.OwnerTableView.ClearEditItems();
        e.Item.OwnerTableView.ClearChildEditItems();
The above code will prevent multiple outstanding add/edit forms where clicking on one insert/update will also automatically validate the other outstanding form (see attached picture). Is there a way to prevent this?

Unfortunately, after adding the above code, none of the grid events (see below) is fired. I was unable to add or update any item after clicking the Insert or Update link.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
    OnNeedDataSource="RadGrid1_NeedDataSource"
    OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
    OnInsertCommand="RadGrid1_InsertCommand"
    OnUpdateCommand="RadGrid1_UpdateCommand"
    OnDeleteCommand="RadGrid1_DeleteCommand"
    OnEditCommand="RadGrid1_EditCommand"
     OnItemCreated="RadGrid1_ItemCreated"
     OnItemDataBound="RadGrid1_ItemDataBound"
      OnItemCommand="RadGrid1_ItemCommand"
       OnPreRender="RadGrid1_OnPreRender"
    >
This is a show stopper for us. Your help is greatly appreciated. I can provide the complete source code if needed.

Thank you.

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Aug 2013, 09:12 AM
Hi Yoong,

Please try using the ItemCommand event rather than ItemDataBound event.I was able to get it working correctly with the code given below, using a Required Field Validator.I was able to get all the events firing.Please have a look and check if it helps.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" PageSize="7"
    AllowPaging="True" OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource"
    OnPreRender="RadGrid1_PreRender" OnItemCommand="RadGrid1_ItemCommand" AutoGenerateEditColumn="true">
    <MasterTableView DataKeyNames="OrderID" InsertItemPageIndexAction="ShowItemOnCurrentPage"
        HierarchyLoadMode="ServerOnDemand" CommandItemDisplay="Top">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="OrderID" Name="OrderDetails" HierarchyLoadMode="ServerOnDemand">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="Unit Price" DataField="UnitPrice">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Quantity" DataField="Quantity">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Freight" DataField="Freight" UniqueName="Freight">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="ShipVia" UniqueName="TemplateColumn">
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("ShipVia") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <br />
                    <asp:TextBox runat="server" ID="tbUnitPrice" Text='<%# Bind("ShipVia") %>'></asp:TextBox>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbUnitPrice"
                        ErrorMessage="This field is required" runat="server" Display="Dynamic" ForeColor="Red">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>  
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
        {
            RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");
        }
    }
 
    protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        switch (e.DetailTableView.Name)
        {
           case "OrderDetails":
                {
                    string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
                    e.DetailTableView.DataSource = GetDataTable("SELECT * FROM [Order Details] WHERE OrderID = '" + OrderID + "'");
                    break;
                }
        }
    }
 
    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);
        DataTable myDataTable = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
 
        return myDataTable;
    }   
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick")
        {
            GridDataItem item = (GridDataItem)e.Item;
            value = item.GetDataKeyValue("CustomerID").ToString();
            foreach (GridItem item1 in e.Item.OwnerTableView.Items)
            {
                if (!(item.Expanded) && (item == e.Item))
                {
                    item.Expanded = true;
                }
            }
        }
 
      //To allow only one Edit or Insert at a time
        if (e.CommandName == RadGrid.EditCommandName)
        {
            RadGrid1.MasterTableView.IsItemInserted = false;
        }
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            RadGrid1.MasterTableView.ClearEditItems();
            e.Item.OwnerTableView.ClearChildEditItems();
        }
    }  

Thanks,
Princy
0
Yoong
Top achievements
Rank 1
answered on 04 Aug 2013, 09:20 PM
Princy,

Thank you for the solution. Greatly appreciate it!

Unfortunately, it only works partially for our requirement. We need to have only one outstanding edit form (either parent or child) but we can now create multiple outstanding form with your solution (see attached image). Is there a way to resolve this?

I also have the complete set of sample code which you can run in the following link
http://www.telerik.com/community/forums/aspnet-ajax/grid/hierarchical-grid-insert-update-do-not-fire-after-isiteminserted-false-and-clearedititems.aspx

0
Princy
Top achievements
Rank 2
answered on 05 Aug 2013, 04:35 AM
Hi Yoong,

Please write the codes for clearing the outstanding forms in ItemCommand event and not in ItemDataBound,you are writing it in ItemDataBound so the other events are not firing.The code is working fine at my side when not writing the code in ItemDataBound but in ItemCommand.Try moving the code to ItemCommand and check if the the issue persist.

Thanks,
Princy
0
Yoong
Top achievements
Rank 1
answered on 05 Aug 2013, 12:45 PM
Princy,

Thanks for the prompt reply!

The code that you saw is the old one. I did exactly as what you provided in ItemCommand and take out all the code in ItemDataBound. The event did fire but unfortunately, it dd not behave the way that we wanted. That is only allow one outstanding edit form at a given time as shown in my previous post. The multiple form outstanding occurs when

  1. When you click Add new subscriber (parent) on the parent, then open up the child list and then Add  new dependent (child). It does not close the parent edit form.
  2. When you open the child list, click on Add new dependent (child) which open up the child edit form for add,  then click on the Edit of another child element which open up another edit form for updating the child element. It does not close the edit form for Add new child.

Putting the code in the ItemDataBound (as shown in my previous post) provides the exact behavior that we wanted but it makes the event such as Insert and Update not firing.

Can you please help resolving this?

Thank you for your help!

-Yoong

0
Andrey
Telerik team
answered on 08 Aug 2013, 07:13 AM
Hello,

Instead of clearing all edit and insert forms after they have been opened why don't you try to prevent them from opening? You could do this in the ItemCommand event. Check whether the command name is InitInsert and Edit and from which TableView the command is initiated and if there is already one item in Edit/Insert mode you would cancel the command.

The problem with the events that are not fired comes from the fact that you clear the Edit/Insert forms and RadGrid does not fire events when there are no items in Edit/Insert mode.

Following the above approach should help you achieve your goal.

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Yoong
Top achievements
Rank 1
answered on 08 Aug 2013, 08:30 PM
Andrey,

Thank you for your suggestion. However, it does not meet the requirement. We need to be able to open a new edit form and automatically close any outstanding one when user click on the Add or Edit link. We cannot prevent user from opening a form when he/she click on the Add or Edit link.

Any solution to meet this requirement?

-Yoong
0
Andrey
Telerik team
answered on 13 Aug 2013, 10:29 AM
Hello,

I am attaching a sample project that is working as expected on my side. Give it a try and you should not have problems.

Regards,
Andrey
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Yoong
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Yoong
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or