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

Grid Editform, UserControl with Event

2 Answers 182 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gerry
Top achievements
Rank 1
Gerry asked on 12 May 2014, 06:48 PM
Hi,

Currently I have a RadGrid that allows me to user a usercontrol to add and edit. My only issue now is that I need the usercontrol to return an object to the page holding the RadGrid. To do this I have created a custom event handler in the usercontrol. At time of edit or insert I assign a local method to the event:

if (e.Item.IsInEditMode)
{
    GridEditFormItem editItem = e.Item as GridEditFormItem;
 
    object EditControl = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID);
    if (EditControl is ValanceProfileCoilLength)
    {
        ValanceProfileCoilLength CL = (ValanceProfileCoilLength)EditControl;
        CL.ItemInserted += new ValanceProfileCoilLength.ItemInsertedEventHandler(CL_ItemInserted);
        REC_ValanceProfileCoilLength ACL;
        if (e.Item.ItemIndex >= 0) ACL = this.VP.ActiveCoilLengths[e.Item.ItemIndex];
        else ACL = new REC_ValanceProfileCoilLength(null, this.ConnString);
         
        CL.Show(ACL);
    }
}


This only good to get the event attached but since the content is dynamic I have to re-attach the method to the ItemInserted event. I can successfully re-attach the method to the event when in insert mode at page_load but I cannot achieve this in edit mode. See code (comments inside to explain the point of failure)

protected void Page_Load(object sender, EventArgs e)
{
    // Re-attach method to events in EditForm UserControls
    // Insert EditForm
    if (this.grdCoilLengths.MasterTableView.IsItemInserted)
    {
        object EditForm = this.grdCoilLengths.MasterTableView.GetInsertItem().FindControl("EditFormControl");
        ValanceProfileCoilLength CL = (ValanceProfileCoilLength)EditForm;
        CL.ItemInserted += new ValanceProfileCoilLength.ItemInsertedEventHandler(CL_ItemInserted);  // <-- This works when in insert mode
    }
 
    // Edit EditForm (Only one edit form allowed at once)
    if (this.grdCoilLengths.EditItems.Count > 0)
    {
        object EditControl = this.grdCoilLengths.EditItems[0].FindControl(GridEditFormInsertItem.EditFormUserControlID); // <-- This returns null when in edit mode
        if (EditControl is ValanceProfileCoilLength)
        {
            ValanceProfileCoilLength CL = (ValanceProfileCoilLength)EditControl;
            CL.ItemInserted += new ValanceProfileCoilLength.ItemInsertedEventHandler(CL_ItemInserted);
        }               
    }
}






2 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 15 May 2014, 03:32 PM
Hello Gerry,

You could try and attach the event handler in the UserControl code-behind. The approach is illustrated in the EditForm Types demo of RadGrid. Check the code in EmployeeDetailsCS.ascx.cs. The attached handler is for the DataBinding event, however you could use similar approach for your event.

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Gerry
Top achievements
Rank 1
answered on 06 Mar 2015, 04:19 PM
This solution worked for me.

http://www.telerik.com/forums/find-grid-editform-user-control-from-outside-the-item-command-method-s

I included the following code to run at page load. This code is on the page that contains the RadGrid with the WebUserControls as EditForms.

// Remap Insert Form
if (this.grdCoilLengths.MasterTableView.IsItemInserted)
{
    object EditForm = this.grdCoilLengths.MasterTableView.GetInsertItem().FindControl("EditFormControl");
    ValanceProfileCoilLength CL = (ValanceProfileCoilLength)EditForm;
    CL.ItemInserted += new ValanceProfileCoilLength.ItemInsertedEventHandler(CL_ItemInserted);
}
 
 
// Remap Edit Form
foreach (GridDataItem item in this.grdCoilLengths.EditItems)
{
    GridEditableItem edititem = (GridEditableItem)item.EditFormItem;
 
    ValanceProfileCoilLength CL = edititem.FindControl(GridEditFormItem.EditFormUserControlID) as ValanceProfileCoilLength;
    CL.ItemInserted += new ValanceProfileCoilLength.ItemInsertedEventHandler(CL_ItemInserted);
}


Tags
Grid
Asked by
Gerry
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Gerry
Top achievements
Rank 1
Share this question
or