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

Change EditForm Controls Dynamically

3 Answers 277 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ReneF
Top achievements
Rank 1
ReneF asked on 14 Aug 2008, 08:42 AM
Hi,
I am using RadGrid ASP.NET Ajax 2008 Q2. Is that possible to change EditForm Controls dynamically on Edit Command depends on current dataType? I have a simple Grid with three columns as follows:
Name                            Value                Type
Installation date              12.12.2008        DateTime
newModemRequired       false                  Boolean
installation by                  myEmployee     String

Purpose:
1.  Select item to Edit.
2.  Get the DataType on selecttion
3.  Render EditForms on CreateColumnEditor
 protected void RadGrid1_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)  
         {  
              //string getDataTypeAtIndex(int index) => Extract Values from Grid item.  
             string myType=getDataTypeAtIndex(RadGrid1.SelectedItems[0].ItemIndex);  
                      
                     if (e.Column.UniqueName == "Value")  
                     {  
                         if (myType =="Boolean")  
                         {  
                             Telerik.Web.UI.GridCheckBoxColumnEditor boolFieldEditor = new Telerik.Web.UI.GridCheckBoxColumnEditor();  
                             e.ColumnEditor = boolFieldEditor;  
                         }  
                         else if (myType == "DateTime")  
                         {  
                             Telerik.Web.UI.GridDateTimeColumnEditor dateFieldEditor = new GridDateTimeColumnEditor();  
                             e.ColumnEditor = dateFieldEditor;  
                         }  
 
                        //String or Integer  
                         else 
                         {  
 
                             Telerik.Web.UI.GridTextBoxColumnEditor textFieldEditor = new Telerik.Web.UI.GridTextBoxColumnEditor();  
                             e.ColumnEditor = textFieldEditor;  
                         }  
                     }  
                 } 

That Code compiles but on runtime if I Click Edit Button I get an error like 'Message from server can not be parsed...'
Thanks for any help

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 14 Aug 2008, 12:25 PM
Hi ReneF,

If you want to replace the default EditForm controls with the Desired controls according to cell value, you can try out the following code.
cs:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
       if(e.Item is GridEditableItem && e.Item.IsInEditMode) 
        {       
               GridEditableItem item=(GridEditableItem)e.Item; 
               TextBox txtbx = (TextBox)item["DataType"].Controls[0]; 
               if (txtbx.Text == "Bool") 
               { 
                   item["DataType"].Controls.Remove(txtbx); 
                   CheckBox checkbox =new CheckBox(); 
                   checkbox.ID = "CheckBox1"
                   item["DataType"].Controls.Add(checkbox); 
               } 
               else if (txtbx.Text == "DateTime") 
               { 
                   item["DataType"].Controls.Remove(txtbx); 
                   RadDateInput date = new RadDateInput(); 
                   date.ID = "RadDateInput1"
                   item["DataType"].Controls.Add(date); 
               } 
        }        
    } 

Thanks
Princy.
0
ReneF
Top achievements
Rank 1
answered on 14 Aug 2008, 02:07 PM
Hi Princy,
Thanks for your reply. I tried your code but the same result. It caused exeption on runtime like 'The message from server could not be parsed' ..
I read the value of dataType Field and change the controls on Value Field.
Here is my code:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
       {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        {  
            GridEditableItem item = (GridEditableItem)e.Item;  
            TextBox txtbx = (TextBox)item["type"].Controls[0];  
            if (txtbx.Text == "Boolean")  
            {  
                item["Value"].Controls.Remove(item["Value"].Controls[0]);  
                CheckBox checkbox = new CheckBox();  
                checkbox.ID = "CheckBox1";  
                item["Value"].Controls.Add(checkbox);  
            }  
            else if (txtbx.Text == "DateTime")  
            {  
                item["Value"].Controls.Remove(item["Value"].Controls[0]);  
                RadDateInput date = new RadDateInput();  
                date.ID = "RadDateInput1";  
                item["Value"].Controls.Add(date);  
            }  
        }  
       } 
Best Regards
0
ReneF
Top achievements
Rank 1
answered on 15 Aug 2008, 01:28 PM
Hi Princy,
Thanks again for your help. I solved the problem. Removing the EditControls in EditMode caused the Exception.
Solution:
1. Create new Controls(DatePicker, CheckBox etc) on ItemDataBound and Set visible property to false for old controls.
2. On Update Command get Values from new created controls and save.

Best Regards
Tags
Grid
Asked by
ReneF
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
ReneF
Top achievements
Rank 1
Share this question
or