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

RadGrid with Custom EditItem IBindableTemplate

0 Answers 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Abhishek
Top achievements
Rank 1
Abhishek asked on 29 Mar 2016, 07:50 AM

I am trying to Implement IBindableTemplate for creating custom Edit Template in Batch Edit Mode. Not able to implement the ExtractValues method of IBindableTemplate interface correctly. When I assign the Control.ID in InstantiateIn() method then i am getting the following error. Without  this control ID how to get the values in ExtractValues() method.

System.Web.HttpException: Multiple controls with the same ID 'DepartmenCode' were found. FindControl requires that controls have unique IDs.   
 at System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls)    
 at System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls)    
 at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)    
 at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)    
 at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

protected override void OnInit(EventArgs e)
        {
RadGrid theGrid = new RadGrid();
                    theGrid.ID = this.ID + "Grid";
                    theGrid.AutoGenerateColumns = false;
theGrid.NeedDataSource += new GridNeedDataSourceEventHandler(theGrid_NeedDataSource);
                    theGrid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(theGrid_DetailTableDataBind);
                    theGrid.SortCommand += new GridSortCommandEventHandler(theGrid_SortCommand);
                    theGrid.ItemDataBound += new GridItemEventHandler(theGrid_ItemDataBound);
                    theGrid.DataBound += new EventHandler(theGrid_DataBound);
                    theGrid.PreRender += new EventHandler(theGrid_PreRender);
                    theGrid.PageIndexChanged += new GridPageChangedEventHandler(theGrid_PageIndexChanged);
                    //Batch Edit Mode Start 
                    theGrid.BatchEditCommand += new GridBatchEditEventHandler(theGrid_BatchEditCommand);
                    
                    theGrid.MasterTableView.EditMode = GridEditMode.Batch;
                    theGrid.MasterTableView.BatchEditingSettings.EditType = GridBatchEditingType.Row;
                    theGrid.MasterTableView.BatchEditingSettings.OpenEditingEvent = GridBatchEditingEventType.DblClick;
                    theGrid.MasterTableView.BatchEditingSettings.SaveAllHierarchyLevels = true;
                    theGrid.MasterTableView.CommandItemSettings.ShowSaveChangesButton = true;
                    theGrid.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = false;
                    theGrid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
                    theGrid.ShowHeader = true;
                    theGrid.MasterTableView.CommandItemSettings.ShowCancelChangesButton = true;
                    //Batch Edit Mode End 

SetupGridColumns();
}

private void SetupGridColumns(){
 
foreach (XmlElement displayField in DisplayFields)
            {
SetupGridColumn(displayField)
}

}


 private void SetupGridColumn(fieldName){
GridTemplateColumn col=new GridTemplateColumn();
                
                SPField field = TopList.Fields[fieldName];
                col.ItemTemplate = new DataGridEditTemplate(ListItemType.Item, fieldName, fieldInternalName, field, theGrid, listView.Utility);
                col.EditItemTemplate = new DataGridEditTemplate(ListItemType.EditItem, fieldName,fieldInternalName, field, theGrid, listView.Utility);
                col.DataField = fieldInternalName;
GridTableView.Columns.Add(col);
}

 private void theGrid_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
        {
            foreach (GridBatchEditingCommand command in e.Commands)
            {
                if ((command.Type == GridBatchEditingCommandType.Update))
                {
                    Hashtable newValues = command.NewValues;
                    Hashtable oldValues = command.OldValues;
                    string ID = newValues["ID"].ToString();
                    //string Name = newValues["Name"].ToString();

                    try
                    {
                        SPList list = SPContext.Current.Web.Lists.TryGetList("Department");
                        SPListItem item = list.GetItemById(Convert.ToInt32(ID));
                        foreach (DictionaryEntry entry in newValues)
                        {
                            if ((string)entry.Key != "ID")
                            {
                                item[(string)entry.Key] = entry.Value;
                            }
                        }
                        item.Update();

                    }
                    catch (Exception exc)
                    {

                        Logger.Log(TraceSeverity.High, this.PageUrl, this.Title, this.CurrentUserName, exc.Message, exc.StackTrace);
#if DEBUG
                        ErrorMessage = Utility.GetMessage(WADataViewerConstant.ResourceNamespace, "ERROR_OCCURED", exc.ToString());
#else
                ErrorMessage = Utility.GetMessage(WADataViewerConstant.ResourceNamespace, "ERROR_OCCURED", exc.Message);
#endif
                        e.Canceled = true;
                    }
                }
            }
        }

public class DataGridEditTemplate : IBindableTemplate
    {
        ListItemType _templateType; 
        string columnName;
        SPField _spField;
        RadGrid _theGrid;
        bool isAutoPostBack;
        string _fieldInternalName;
        WAUtility _Utility;
        public DataGridEditTemplate(ListItemType templateType, string colname, string fieldInternalName, SPField field, RadGrid grid, WAUtility Utility)
        {
            _templateType = templateType;
            columnName = colname;
            _fieldInternalName = fieldInternalName;
            _spField = field;
            _theGrid = grid;
            _Utility = Utility;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            Label lc = new Label();
            switch (_templateType)
            {
              
                case ListItemType.Item:
                   // lc.Text = "Item " + _fieldInternalName;
                    lc.DataBinding += new EventHandler(lControl_DataBinding);
                    //lc.ID =_fieldInternalName;
                    container.Controls.Add(lc);
                    break;
                case ListItemType.EditItem:
                  
                    container.Controls.Add(GetControlByFieldType(_spField, null));
                    break;
                  
            }
        }
        public void lControl_DataBinding(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            GridDataItem container = (GridDataItem)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)[_fieldInternalName].ToString();
        }

private Control GetControlByFieldType(SPField field)
        {
            Control fieldControl = null;
            BaseFormControl basefc = null;

            string validationGroup = field.Id + "ValidationGroup";
            string mask = null;
            string promptCharacter = null;

            bool editable = !field.ReadOnlyField; 
            //bool autoPostBack = IsAutoPostbackField(fieldNode);

            switch (field.Type)
            {
                case SPFieldType.Text:
                    #region -------------Text---------------------- 
                    SPFieldText fieldText = (SPFieldText)field;

                    TextFieldControl tfc = new TextFieldControl();
                    basefc = tfc;
                    tfc.ID = _fieldInternalName;
                    tfc.ContainerID = validationGroup;
                    tfc.Mask = mask;
                    tfc.PromptCharacter = promptCharacter;
                    tfc.FieldMaxLength = fieldText.MaxLength;
                    tfc.FieldDisplaySize = fieldText.DisplaySize;
                    tfc.FieldIMEMode = fieldText.IMEMode;
                    tfc.Editable=editable;
                    fieldControl = tfc;

                    break;
                    #endregion -------------Text---------------------- 
}
}

  public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
        {
            System.Collections.Specialized.OrderedDictionary od = null;
            //od.Add("OrderID", ((DropDownList)(((GridEditFormItem)(container)).FindControl("dropdownlist1"))).DataValueField);
            //if (container is GridDataItem)
            //{
            //    GridDataItem dataItem = container as GridDataItem;
            //}
            //if (container is GridEditFormItem)
            {
                od = new System.Collections.Specialized.OrderedDictionary();
                //od.Add(_fieldInternalName, ((DropDownList)(((GridEditFormItem)(container)).FindControl("_fieldInternalName"))).DataValueField);
                //Control ctrl = ((container)).FindControl("_fieldInternalName");

                foreach (Control ctrl in container.Controls)
                {
                    if (ctrl.Controls.Count > 0)
                    {
                        Type ctrlType = ctrl.Controls[0].GetType();
                        if (ctrlType == typeof(DropDownList))
                        {
                            od.Add(ctrl.Controls[0].UniqueID, ((DropDownList)(ctrl.Controls[0])).DataValueField);
                        }
                        if (ctrlType == typeof(TextBox))
                        {
                            od.Add(ctrl.Controls[0].UniqueID, ((TextBox)(ctrl.Controls[0])).Text);
                        }
                        if (ctrlType == typeof(Label))
                        {
                            od.Add(ctrl.Controls[0].UniqueID, ((Label)(ctrl.Controls[0])).Text);
                        }
                    }
                }
            }
            
            
            return od;
        }
    }

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Abhishek
Top achievements
Rank 1
Share this question
or