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

Update command event is not fire in dynamic create grid

3 Answers 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Harsh Verma
Top achievements
Rank 1
Harsh Verma asked on 26 Mar 2010, 03:20 PM
Hi,
     guys i am creating a web control and in this web control,  i am creating a dynamic rad grid But Update command event  is not fire.
    and neither any other command is not working in edit row.
 
     i want to tell you ,  i  am create a object of rad grid and add template fields in a grid on Init event of this webcontrol. edit command is working  fine but update command is not working . and choose  EditMode = InPlace , i am Binding this grid in !Page.Ispostback
and i am also use NeedDataSource event handler.

     Please help me this is urgent .
     Thanks

 override protected void OnInit(EventArgs e)
        {
            radGrid = new RadGrid();

            radGrid.EditCommand += new GridCommandEventHandler(radGrid_EditCommand);
            radGrid.UpdateCommand += new GridCommandEventHandler(radGrid_UpdateCommand);
            radGrid.CancelCommand += new GridCommandEventHandler(radGrid_CancelCommand);
            radGrid.NeedDataSource += new GridNeedDataSourceEventHandler(radGrid_NeedDataSource);
            radGrid.ItemDataBound += new GridItemEventHandler(radGrid_ItemDataBound);
            radGrid.ItemCommand += new GridCommandEventHandler(radGrid_ItemCommand);

            populatePropertyOfGrid();
        }
          public void  populatePropertyOfGrid()
        {
            radGrid.ID = "radGrid";
            radGrid.Skin = this.Skin;
            radGrid.EnableEmbeddedSkins = this.EnableEmbeddedSkins;
            radGrid.Width = Unit.Percentage(100);
            radGrid.AutoGenerateColumns = false;
            radGrid.MasterTableView.EditMode = GridEditMode.InPlace;

            string[] DataKeys = new string[5];
            DataKeys[0] = "PageConfigId";
            DataKeys[1] = "IdDetailSearch";
            DataKeys[2] = "IdSearchSearch";
            DataKeys[3] = "DataflowId";
            DataKeys[4] = "IsMandatoryForExport";

            radGrid.MasterTableView.DataKeyNames = DataKeys;

            GridTemplateColumn gridTemplateEditLink = new GridTemplateColumn();
            gridTemplateEditLink.ItemTemplate = new FeildsGridTemplate(ListItemType.Item, "LinkButton", "linkEdit", Skin, "edit", "Edit", null);
            gridTemplateEditLink.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem, "LinkButton", "linkUpdate", Skin, "update", "Update", null);
            gridTemplateEditLink.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            gridTemplateEditLink.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
            gridTemplateEditLink.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            gridTemplateEditLink.ItemStyle.VerticalAlign = VerticalAlign.Middle;
            radGrid.MasterTableView.Columns.Add(gridTemplateEditLink);

            GridTemplateColumn gridTemplateDataElement = new GridTemplateColumn();

            gridTemplateDataElement.ItemTemplate=new FeildsGridTemplate(ListItemType.Item, "Label", "lblDataElement", Skin, "DataElement", "", null);
            gridTemplateDataElement.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem , "Label", "lblEditDataElement", Skin, "DataElement", "", null);
            gridTemplateDataElement.HeaderText = "Data Element";
           // gridTemplateDataElement.SortExpression = "DataElement";
            gridTemplateDataElement.DataField = "DataElement";
            gridTemplateDataElement.HeaderStyle.Width = Unit.Pixel(120);
            gridTemplateDataElement.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            gridTemplateDataElement.HeaderStyle.VerticalAlign = VerticalAlign.Middle;

            gridTemplateDataElement.ItemStyle.Width = Unit.Pixel(120);
            gridTemplateDataElement.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            gridTemplateDataElement.ItemStyle.VerticalAlign = VerticalAlign.Middle;

            radGrid.MasterTableView.Columns.Add(gridTemplateDataElement);



            GridTemplateColumn gridTemplateAvail = new GridTemplateColumn();

            gridTemplateAvail.ItemTemplate = new FeildsGridTemplate(ListItemType.Item, "CheckBox", "chkItemAvailable", Skin, "IsAvailable", "", null);
            gridTemplateAvail.EditItemTemplate = new FeildsGridTemplate(ListItemType.EditItem, "CheckBox", "chkEditAvailable", Skin, "IsAvailable", "", null);

            gridTemplateAvail.HeaderText = "Avail.";
           // gridTemplateAvail.SortExpression = "IsAvailable";
            gridTemplateAvail.DataField = "IsAvailable";
            gridTemplateAvail.HeaderStyle.Width = Unit.Pixel(20);
            gridTemplateAvail.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            gridTemplateAvail.HeaderStyle.VerticalAlign = VerticalAlign.Middle;

            gridTemplateAvail.ItemStyle.Width = Unit.Pixel(20);
            gridTemplateAvail.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            gridTemplateAvail.ItemStyle.VerticalAlign = VerticalAlign.Middle;

            radGrid.MasterTableView.Columns.Add(gridTemplateAvail);


}



   public class FeildsGridTemplate : IBindableTemplate    // IBindableTemplate
    {
        ListItemType templateType;
        string addedControlType;
        string controlId;
        string commandName;
        string Skin;
        string columnName;

        object ControlInput;

        public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
        {
            OrderedDictionary od = new OrderedDictionary();
            // od.Add("ProviderName", ((TextBox)(((GridEditFormItem)(container)).FindControl("MyTextBox"))).Text);
            return od;
        }



        /// <summary>
        /// This Contructor for populate Item,Footer,Edit Property
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pr_ControlType"></param>
        /// <param name="pr_controlId"></param>
        /// <param name="pr_GridId"></param>
        /// <param name="pr_columnName"></param>
        public FeildsGridTemplate(ListItemType type, string pr_ControlType, string pr_controlId, string pr_Skin, string pr_columnName, string pr_commandName, object pr_ControlInput)
        {
            templateType = type;
            addedControlType = pr_ControlType;
            controlId = pr_controlId;
            Skin = pr_Skin;
            columnName = pr_columnName;
            commandName = pr_commandName;
            ControlInput = pr_ControlInput;
        }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            CheckBox chkBox = new CheckBox();
            LinkButton lnkBtn = new LinkButton();
            //CustomizeTextBox txtBox = new CustomizeTextBox();
            TextBox txtBox = new TextBox();
            Label lbl = new Label();
           // DropDownControl dropDownControl = new DropDownControl();
            System.Web.UI.WebControls.DropDownList dropDownControl = new System.Web.UI.WebControls.DropDownList();


            switch (templateType)
            {
                case ListItemType.EditItem:
                    #region EditItem
                    if (addedControlType == "CheckBox")
                    {
                        chkBox.ID = controlId;
                        chkBox.Text = columnName;
                        container.Controls.Add(chkBox);
                        chkBox.DataBinding += new EventHandler(boolValueEdit_DataBinding); // This Event for bind this object

                    }
                    else if (addedControlType == "TextBox")
                    {
                        txtBox.Width = 150;
                      //  txtBox.Connectionstring = Skin; // This is connection string for dataBase connection
                        txtBox.ID = controlId;
                        txtBox.Text = columnName;
                      
                        txtBox.DataBinding += new EventHandler(txtBoxEditDataBinding);
                        container.Controls.Add(txtBox);
                    }
                    else if (addedControlType == "LinkButton")
                    {
                        HtmlTable table = new HtmlTable();
                        table.Attributes.Add("class", "select_bdr");
                      
                        table.CellPadding = 3;
                        table.CellSpacing = 3;

                        HtmlTableRow tr = new HtmlTableRow();
                        HtmlTableCell td1 = new HtmlTableCell();
                        td1.Style.Value = "border-style:none !important;";

                        HtmlTableCell td2 = new HtmlTableCell();
                        td2.Style.Value = "border-style:none !important;";

                        Label td1GapLeft = new Label();
                        Label td1GapRight = new Label();
                        Label td2GapLeft = new Label();
                        Label td2GapRight = new Label();

                        td1GapLeft.Text = "[";
                        td1GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                        td1GapRight.Text = "]";
                        td1GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                        td2GapLeft.Text = "[";
                        td2GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                        td2GapRight.Text = "]";
                        td2GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                        lnkBtn.ID = controlId;
                        lnkBtn.Text = columnName;
                        lnkBtn.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                        lnkBtn.CommandName = "Update";

                        td1.Controls.Add(td1GapLeft);
                        td1.Controls.Add(lnkBtn);
                        td1.Controls.Add(td1GapRight);
                        tr.Controls.Add(td1);
                       

                        LinkButton linkCancel = new LinkButton();
                        linkCancel.ID = "linkCancel";
                        linkCancel.Text = "cancel";
                        linkCancel.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                        linkCancel.CommandName = "Cancel";

                        td2.Controls.Add(td2GapLeft);
                        td2.Controls.Add(linkCancel);
                        td2.Controls.Add(td2GapRight);
                        tr.Controls.Add(td2);
                       
                      

                        table.Controls.Add(tr);
                        container.Controls.Add(table);
                    }
                    else if (addedControlType == "Label")
                    {
                        lbl.ID = controlId;
                        lbl.Text = columnName;
                        lbl.DataBinding += new EventHandler(lblEdit_DataBinding);
                        container.Controls.Add(lbl);
                    }
                   else if (addedControlType == "Dropdown")
                    {
                        dropDownControl.ID = controlId;
                        dropDownControl.SkinID = columnName;
                        DropdownPropertyClass dropdownPropertyClass = (DropdownPropertyClass)ControlInput;
                        if (Skin != "")
                        {
                          //  dropDownControl.DropDownListSkin = dropdownPropertyClass.Skin;
                          //  dropDownControl.EnableEmbeddedSkins = dropdownPropertyClass.EnableEmbeddedSkins;
                        }

                      //  dropDownControl.ConnectionString = dropdownPropertyClass.ConnectionString;

                     //  dropDownControl.MainTableCssClass = "select_bdr";

                       dropDownControl.DataTextField= dropdownPropertyClass.DataTextField;
                       dropDownControl.DataValueField = dropdownPropertyClass.DataValueField;

                    //  if (dropdownPropertyClass.DataSource != null)
                    //  {
                          //dropDownControl.DataSource = dropdownPropertyClass.DataSource;
                         // dropDownControl.DataBind();
                      
                    //  }


                     //   dropDownControl.DataBinding += new EventHandler(dropDownControlEdit_DataBinding);
                        container.Controls.Add(dropDownControl);
                    }
                    break;
                    #endregion
                case ListItemType.Item:
                    #region Item
                    if (addedControlType == "CheckBox")
                    {
                        chkBox.ID = controlId;
                        chkBox.Enabled = false;
                        chkBox.Text = columnName;
                        container.Controls.Add(chkBox);
                        chkBox.DataBinding += new EventHandler(boolValue_DataBinding); // This Event for bind this object

                    }
                    else if (addedControlType == "LinkButton")
                    {
                        HtmlTable table = new HtmlTable();
                        table.Attributes.Add("class","select_bdr");
                        
                        table.CellPadding =0;
                        table.CellSpacing =0;

                        HtmlTableRow tr = new HtmlTableRow();
                        HtmlTableCell td1 = new HtmlTableCell();
                        td1.Style.Value = "border-style:none !important;";
                        Label td1GapLeft = new Label();
                        Label td1GapRight = new Label();
                      

                        td1GapLeft.Text = "[";
                        td1GapLeft.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                        td1GapRight.Text = "]";
                        td1GapRight.Style.Value = "font-size: 10px ! important;color:#0000EE;";

                      
                        lnkBtn.ID = controlId;
                        lnkBtn.Text = columnName;
                        lnkBtn.Style.Value = "font-size: 10px ! important;color:#0000EE; margin-left:3px; margin-right:3px;";
                        lnkBtn.CommandName = "Edit";

                        td1.Controls.Add(td1GapLeft);
                        td1.Controls.Add(lnkBtn);
                        td1.Controls.Add(td1GapRight);
                        tr.Controls.Add(td1);
                      

                     
                        table.Controls.Add(tr);
                        container.Controls.Add(table);

                    }
                    else if (addedControlType == "Label")
                    {
                        lbl.ID = controlId;
                        lbl.Text = columnName;
                        lbl.DataBinding += new EventHandler(lbl_DataBinding);
                        container.Controls.Add(lbl);
                    }
                 
                    #endregion
                    break;
            }

        }


        /// <summary>
        /// This Event for populating Label Text Template Item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            string id = lbl.ID;
            GridDataItem container = (GridDataItem)lbl.NamingContainer;
            if (lbl != null && lbl.Text != "")
            {
                try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
                {
                    lbl.Text = Convert.ToString(((DataRowView)container.DataItem)[lbl.Text]);
                }
                catch (Exception ex)
                {

                }
            }
        }


        /// <summary>
        /// This checkbox event of item template
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void boolValue_DataBinding(object sender, EventArgs e)
        {
            CheckBox cBox = (CheckBox)sender;
            GridDataItem container = (GridDataItem)cBox.NamingContainer;
            if (cBox.Text != "")
            {
                try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
                {
                    cBox.Checked = (bool)((DataRowView)container.DataItem)[cBox.Text];
                }
                catch (Exception ex)
                {

                }
                cBox.Text = "";
            }

            // cBox.Text = "";
        }

        /// <summary>
        /// This Event to Populate Template Edit Dropdown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropDownControlEdit_DataBinding(object sender, EventArgs e)
        {
            RadComboBox dropDownControl = (RadComboBox)sender;
            GridEditableItem container = (GridEditableItem)dropDownControl.NamingContainer;
            if (dropDownControl != null && dropDownControl.SkinID != "")
            {
                try
                {
                    dropDownControl.SelectedItem.Text = Convert.ToString(((DataRowView)container.DataItem)[dropDownControl.SkinID]);
                }
                catch (Exception ex)
                {

                }
            }
        }

        /// <summary>
        /// This checkbox event of Edit template
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void boolValueEdit_DataBinding(object sender, EventArgs e)
        {
            CheckBox cBox = (CheckBox)sender;
            GridEditableItem container = (GridEditableItem)cBox.NamingContainer;
            if (cBox.Text != "")
            {
                try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
                {
                    cBox.Checked = (bool)((DataRowView)container.DataItem)[cBox.Text];
                }
                catch (Exception ex)
                {

                }
                cBox.Text = "";
            }

            // cBox.Text = "";
        }


        /// <summary>
        /// This Event for populating Label Text Template Edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblEdit_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            GridEditableItem container = (GridEditableItem)lbl.NamingContainer;
            if (lbl != null && lbl.Text != "")
            {
                try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
                {
                    lbl.Text = Convert.ToString(((DataRowView)container.DataItem)[lbl.Text]);
                }
                catch (Exception ex)
                {

                }
            }
        }


        /// <summary>
        /// This event For Binding edit TextBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtBoxEditDataBinding(object sender, EventArgs e)
        {
            //CustomizeTextBox txtBoxItem = (CustomizeTextBox)sender;
            TextBox txtBoxItem = (TextBox)sender;
            GridEditableItem container = (GridEditableItem)txtBoxItem.NamingContainer;
            if (txtBoxItem != null && txtBoxItem.Text != "")
            {
                try  // User Don't want to Bind this object, then dataColumn is not found in dataSource then runtime exception occur
                {
                    txtBoxItem.Text = Convert.ToString(((DataRowView)container.DataItem)[txtBoxItem.Text]);
                }
                catch (Exception ex)
                {

                }
            }

        }

    }

    [Serializable()]
    public class DropdownDataSource
    {
        public string PageConfigId = "";
        public DataTable DataSource = null;
   

    }

}



3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 31 Mar 2010, 03:10 PM
Hi Harsh,

Could you please check if using GridEditCommandColumn for the Edit/Update buttons makes any difference? And you can check how it goes with the auto-generated edit command column by setting the AutoGenerateEditColumn property of RadGrid to true.

Let me know how it goes.

Best wishes,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
RadTony
Top achievements
Rank 1
answered on 31 Mar 2010, 04:23 PM
Hi Harsh,

       Seems as if it is impossible to update a dynamic RadGrid (see my post dated 31 march 2010 : Is it possible to update a dynamic grid ?).
       I'm also working on it since 10 days ;-)
       Finally, I'm reverting to the basic GridView, using the following sample :

       http://aspalliance.com/1125_Dynamically_Templated_GridView_with_Edit_Delete_and_Insert_Options.all

       And as far, I'm advancing in my work

Good luck Indian bro ;-)
RadTony      
0
Iana Tsolova
Telerik team
answered on 06 Apr 2010, 01:36 PM
Hello,

Indeed, it is possible to perform insert/update/delete database operations through dynamically created grid. However please make sure you followed the steps on how to add RadGrid at runtime on the page described here.

Furthermore, I used the code provided above in this thread in the attached sample and I observed that the UpdateCommand is properly fired on my end. Can you check it out and let me know if I missed something out?

Kind regards,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Harsh Verma
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
RadTony
Top achievements
Rank 1
Share this question
or