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

Dynamically Changing EditFormItem TemplateControl

5 Answers 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 19 Jun 2013, 08:33 AM
Hi, based on some info stored in the object bound to the radgrid row, I would like to display a different edit form. 

The forms are in ascx user controls. Can I change the TemplateControl onitemcreated? I have only seen examples of doing the on page load or init. 




5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 19 Jun 2013, 08:51 AM
Hi David,

Please try the following code snippet.Hope this helps you.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
 {
 GridEditFormItem item = (GridEditFormItem)e.Item;
 UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
 }
}

Thanks,
Princy
0
David
Top achievements
Rank 1
answered on 19 Jun 2013, 08:59 AM
Hi Percy, 
             I have that, but once I get hold of the control I want to change it to a different user control. 
I is it the Template control I would need to do this in?

var c = e.Item.FindControl(GridEditFormItem.EditFormUserControlID).NamingContainer;
c.TemplateControl.Controls.Clear();
c.TemplateControl.Controls.Add(TemplateControl.LoadControl("~/Controls/Assessments/ctrlNewControl.ascx"));
0
David
Top achievements
Rank 1
answered on 19 Jun 2013, 10:06 AM
ok I guess what I am trying to do is this, but this is for the page_init? is there an equivalent to set it in itemcreated?
rdgAssessments.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template;                  
rdgAssessments.MasterTableView.EditItemTemplate = template;
rdgAssessments.MasterTableView.Columns.Add(new GridEditCommandColumn());
0
Accepted
Princy
Top achievements
Rank 1
answered on 21 Jun 2013, 06:59 AM
Hi David,

I am not sure about the requirement. I guess you want to change the UserControl in edit form to a different one. Here is  a sample code snippet I tried to use different usercontrol for Edit form depending upon the value of a cell.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName) 
    
        GridDataItem item = (GridDataItem)e.Item;
        if (item["OrderID"].Text == "10280")
        {
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "WebUserControl1.ascx";
        }
        else
        {
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "WebUserControl2.ascx";
        }             
    
}

Thanks,
Princy
0
David
Top achievements
Rank 1
answered on 21 Jun 2013, 08:19 AM
Hi Princy, 
That was just what I was looking for! 

Thanks 

David 
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or