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

[Solved] Dynamically loaded usercontrol in formtemplate

4 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Per
Top achievements
Rank 1
Per asked on 11 Feb 2010, 01:47 PM
I am trying to dynamically load a usercontrol in the edit formtemplate of a grid, I only want a part of the template to change. I do that in the grids OnItemCreated event, So far so good. The usercontrol shows and I am able to enter the desired data.

Problem begins when I try to get hold of the usercontrol to actually save the data.

On clicking the update/save button of the formtemplate i want to connect to the usercontrol, get and save the content to the database.

I have tried numerous events and methods found in the forum, but i can't get hold of the control. I suspect this has to do with me adding the control dynamically, and closing the formtemplate results in a postback.

What is the best approach for this kind of scenario.

//Per Löfstedt

4 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 16 Feb 2010, 03:08 PM
Hi Per,

Please review this help topic to find how you can access control in RadGrid FormTemplate edit form upon update button click. It would also be of help, if you can send us the grid ItemCreated and UpdateCommand/ItemCommand event handlers code.

Additionally, I would suggest you to consider using UserControl edit form for your grid.

Check it out and 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
Jan Balas
Top achievements
Rank 1
answered on 20 Feb 2010, 07:07 PM
Hi Iana,
I have the same problem as Per - when i put controls into gridview, I can see them on webpage, but I cannot access them during in inset command. Please see my code below (it is stripped as much as possible, but I hope it still demonstrates my problem)


protected void PatientsGrid_ItemDataBound(object sender, GridItemEventArgs e) 
 { 
   if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
    { 
     GridEditFormItem editFormItem = e.Item as GridEditFormItem; 
     GridView gv = editFormItem.FindControl("gvPatientProperties"as GridView; 
     GeneratePropertyControls(gv); 
    } 
 } 

Put controls into the grid
void GeneratePropertyControls(GridView GvProperties) 
    DataTable gridSource = new DataTable(); 
    DataColumn colName = new DataColumn(); 
    gridSource.Columns.Add(colName); 
    TableCell gridTc = new TableCell(); 
    TableRow gridTr = new TableRow(); 
    gridTr.Cells.Add(gridTc); 
 
  for (int c = 0; c < 10; c++) 
   { 
      gridSource.Rows.Add(gridTr); 
   } 
 
  GvProperties.DataSource = gridSource; 
  GvProperties.DataBind(); 
 
  foreach (DataRow dr in dtGroups.Rows) 
   { 
      TextBox tb = new TextBox(); 
      TableCell cell = new TableCell(); 
      GvProperties.Rows[i].Cells.Add(cell); 
      GvProperties.Rows[i].Cells[0].Controls.Add(tb); 
   } 

protected void PatientsGrid_InsertCommand(object source, GridCommandEventArgs e) 
 { 
   for (int i = 0; i < gvPatientProperties.Rows.Count; i++) 
    { 
        if (gvPatientProperties.Rows[i].Cells[0].Controls.Count > 0) 
         { 
           TextBox tx = gvPatientProperties.Rows[i].Cells[0].Controls[0] as TextBox  
           //Write value to database 
          } 
     } 
 } 
 
GridView always has the same number of rows as it's datasource (10 in this example) but, Controls.Count is 0.

Jan Balas

 
0
Princy
Top achievements
Rank 2
answered on 22 Feb 2010, 05:50 AM
Hi,

Please add  the code to dynamically populate the GridView with controls in the ItemCreated event. The ItemCreated and not the ItemDataBound event is fired on every postback, and so  the control collection will persist.

C#
protected void PatientsGrid_ItemCreated(object sender, GridItemEventArgs e)  
 {  
   if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
    {  
     GridEditFormItem editFormItem = e.Item as GridEditFormItem;  
     GridView gv = editFormItem.FindControl("gvPatientProperties"as GridView;  
     GeneratePropertyControls(gv);  
    }  
 }  

Thanks,
Princy
0
Jan Balas
Top achievements
Rank 1
answered on 23 Feb 2010, 05:59 PM
Hi Princy,
thank you for your answer. Thai was exactly what I needed.

Jan Balas
Tags
Grid
Asked by
Per
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Jan Balas
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or