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

[Solved] UserControl Insert/Edit/Update

1 Answer 300 Views
Grid
This is a migrated thread and some comments may be shown as answers.
abouzar
Top achievements
Rank 1
abouzar asked on 23 Sep 2009, 08:32 AM
Hello
I have a WebControl Project ,it has many class that Create Dynamic control ,for example TextBox,checkBox and so on.
in my project Generat WebForm via this project and i have a class name "Grid"
in this class I create a RadGrid and Bind to diffrent Table
I want to Insert/Delete/Update via Grid and UserControl
when I Click "Add new Record" in top Grid:NeedDataSource And ItemCommand Event Raised and Load UserControl
but I Click Save Button with "PerformInsert" CommandName just NeedDataSource And ItemCommand Event Raised and InsertCommand Event not raise
Thanks a lot
 public override System.Web.UI.Control GetControl(System.Data.DataRow dr)  
        {  
            ItemData = dr;  
            Telerik.Web.UI.RadAjaxPanel pnl = new Telerik.Web.UI.RadAjaxPanel();  
            //System.Web.UI.WebControls.Panel pnl = new System.Web.UI.WebControls.Panel();  
            pnl.Width = this.Width;              
            pnl.ID = this.ID;  
            pnl.Visible = this.Visible;  
            pnl.Attributes.Add("Style"string.Format("position: absolute;left: {0}px; Top:{1}px;z-index:{2};"this.Left, this.Top , this.zIndex));  
            pnl.Width = this.Width;  
            pnl.Height = this.Height;  
            Telerik.Web.UI.RadGrid _RadGrid = new Telerik.Web.UI.RadGrid();  
            _RadGrid.Width = this.Width;  
            _RadGrid.Height = this.Height;  
            _RadGrid.Visible = this.Visible;  
            _RadGrid.ClientSettings.Scrolling.AllowScroll = true;  
            _RadGrid.ID = "grd_" + this.ID;  
            //_RadGrid.AllowAutomaticInserts = true;  
            //_RadGrid.AllowAutomaticUpdates = true;  
              
            if (!this.ReadOnly)  
            {  
                _RadGrid.MasterTableView.CommandItemDisplay = Telerik.Web.UI.GridCommandItemDisplay.Top;  
                _RadGrid.MasterTableView.CommandItemSettings.AddNewRecordText = "اضافه کردن رکورد جدید";  
                _RadGrid.MasterTableView.CommandItemSettings.RefreshText = "بروز رسانی";  
                _RadGrid.Attributes.Add("Style"string.Format("position: absolute;left: {0}px; Top:{1}px;", 0, 0));  
                _RadGrid.Height = this.Height;  
                //_RadGrid.MasterTableView.EditMode = Telerik.Web.UI.GridEditMode.InPlace;  
              
            }              
            //_RadGrid.MasterTableView.EditFormSettings.PopUpSettings. ZIndex = 15000;  
            _RadGrid.MasterTableView.EditFormSettings.FormStyle.BackColor = System.Drawing.Color.WhiteSmoke;  
             _RadGrid.Skin = "Office2007";  
            _RadGrid.MasterTableView.EditFormSettings.EditFormType = Telerik.Web.UI.GridEditFormType.WebUserControl;  
            //_RadGrid.ClientSettings.ClientEvents.OnPopUpShowing = "PopUpShowing"; // this function must exist in Master Page ;)  
            _RadGrid.MasterTableView.EditFormSettings.UserControlName = "~/FormGeneratorTools/UCLoadForm.ascx";  
            _RadGrid.MasterTableView.DataKeyNames =new string[1]{string.Format("frm{0}Id", BindingFormID)};  
            _RadGrid.NeedDataSource += new GridNeedDataSourceEventHandler(_RadGrid_NeedDataSource);  
            _RadGrid.ItemCommand += new GridCommandEventHandler(_RadGrid_ItemCommand);  
            _RadGrid.InsertCommand += new GridCommandEventHandler(_RadGrid_InsertCommand);  
            _RadGrid.UpdateCommand += new GridCommandEventHandler(_RadGrid_UpdateCommand);  
            _RadGrid.DeleteCommand += new GridCommandEventHandler(_RadGrid_DeleteCommand);  
            AddcolumntoGrid(_RadGrid);  
            _RadGrid.AutoGenerateColumns = false;  
            object objj = _RadGrid.DataSource;  
            pnl.Controls.Add(_RadGrid);  
            return pnl;  
        }  
 
        void _RadGrid_DeleteCommand(object source, GridCommandEventArgs e)  
        {  
            string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex][string.Format("frm{0}Id", BindingFormID)].ToString();  
            if (((RadGrid)source).Page.Session["Data_" + BindingFormID] != null)  
            {  
                System.Data.DataSet ds = (System.Data.DataSet)((RadGrid)source).Page.Session["Data_" + BindingFormID];  
                if (ds.Tables[0].Rows.Find(ID) != null)  
                {  
                    ds.Tables[0].Rows.Find(ID).Delete();  
                    ds.Tables[0].AcceptChanges();  
                }  
            }  
        }  
 
        void _RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            System.Data.DataSet ds = new System.Data.DataSet();  
             if (e.RebindReason == GridRebindReason.InitialLoad)  
                 ((RadGrid)source).Page.Session["Data_" + BindingFormID] = GetData();   
 
             if (((RadGrid)source).Page.Session["Data_" + BindingFormID] != null)  
                 ds.Merge((System.Data.DataSet)((RadGrid)source).Page.Session["Data_" + BindingFormID]);  
                   
            if(ds!=null)   
                ((RadGrid)source).DataSource = ds;  
        }          
        void _RadGrid_InsertCommand(object source, GridCommandEventArgs e)  
        {  
             
            GridEditableItem editedItem = e.Item as GridEditableItem;  
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);  
            object obj = userControl.FindControl("Co_" + BindingFormID);  
            System.Data.DataSet dsNewData = ((WebForm)obj).GetDate(userControl.Request);  
            string CurrentID = "0";  
            if (userControl.Session["Data_" + BindingFormID] != null)  
            {  
                if((((System.Data.DataSet)userControl.Session["Data_" + BindingFormID])).Tables[0].Rows.Count!=0)  
                    CurrentID = (((System.Data.DataSet)userControl.Session["Data_" + BindingFormID])).Tables[0].Select(string.Format("frm{0}Id > 0", BindingFormID), string.Format("frm{0}Id DESC", BindingFormID))[0][string.Format("frm{0}Id", BindingFormID)].ToString();  
                dsNewData.Tables[0].Rows[0][string.Format("frm{0}Id", BindingFormID)] = long.Parse(CurrentID) + 1;  
                dsNewData.Merge((System.Data.DataSet)userControl.Session["Data_" + BindingFormID]);  
            }  
            else 
                dsNewData.Tables[0].Rows[0][string.Format("frm{0}Id", BindingFormID)] = long.Parse(CurrentID);  
 
            userControl.Session["Data_" + BindingFormID] = dsNewData;  
     
        }  
        /// <summary>  
        ///   
        /// </summary>  
        /// <param name="source"></param>  
        /// <param name="e"></param>  
        void _RadGrid_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            ((Telerik.Web.UI.GridBaseDataList)(source)).Page.Session["FormID"] = BindingFormID;  
        }  
        /// <summary>  
        ///   
        /// </summary>  
        /// <param name="source"></param>  
        /// <param name="e"></param>  
        void _RadGrid_UpdateCommand(object source, GridCommandEventArgs e)  
        {  
 
            GridEditableItem editedItem = e.Item as GridEditableItem;  
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);  
            object obj = userControl.FindControl("Co_" + BindingFormID);  
            System.Data.DataSet dsNewData = ((WebForm)obj).GetDate(userControl.Request);  
            string ID = (e.Item as GridEditFormItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex][string.Format("frm{0}Id", BindingFormID)].ToString();  
            if (userControl.Session["Data_" + BindingFormID] != null)  
            {  
                dsNewData.Tables[0].Rows[0][string.Format("frm{0}Id", BindingFormID)] = ID;  
                if(((System.Data.DataSet)userControl.Session["Data_" + BindingFormID]).Tables[0].Rows.Find(ID)!=null)  
                    ((System.Data.DataSet)userControl.Session["Data_" + BindingFormID]).Tables[0].Rows.Find(ID).Delete();  
                ((System.Data.DataSet)userControl.Session["Data_" + BindingFormID]).AcceptChanges();  
                dsNewData.Merge((System.Data.DataSet)userControl.Session["Data_" + BindingFormID]);  
            }  
            userControl.Session["Data_" + BindingFormID] = dsNewData;  
              
        } 

1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 26 Sep 2009, 06:08 AM
Hi abouzar,

This behavior is not expected as evidenced by our online example featuring a user control for the grid's edit form:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/usercontroleditform/defaultcs.aspx

Unfortunately the code snippets you have provided do not supply enough information as to what the problem might be. I'd suggest that you open a formal support ticket and attach your project for inspection and debugging.

Thank you.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
abouzar
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or