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

Dynamic RadGrid with EditItemTemplate

4 Answers 420 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jasminder
Top achievements
Rank 1
Jasminder asked on 28 Nov 2011, 01:04 PM
Hello,

I am creating a dynamic RadGrid completely in the code. I am adding the buttons for ItemTemplates and EditItemTemplates for the Editing the controls. I am creating this in the Page_Init function.

The controls binds fine and displays the data and "Edit Button". But the problem is that when I click on the "Edit" button, the "ItemCommand" event does not fires for the grid and The grid is not displayed in edit mode. The Page only postback to the server and binds the grid again. But if i click on the "Delete" button, which is "BoundColumn", it does fire the "ItemCommand" event for the grid. What point I may be missing here ?

 Following is the code for it :

           
public class ItemTemplateLinks : ITemplate
 {
     protected RadButton rBtn;
     string colname = string.Empty;
     string colValue = string.Empty;
     public ItemTemplateLinks(string cName)
     {
         colname = cName;
     }
     public void InstantiateIn(System.Web.UI.Control container)
     {
         rBtn = new RadButton();
         rBtn.Text = "Edit";
         rBtn.ButtonType = RadButtonType.StandardButton;
         rBtn.Command += new CommandEventHandler(rBtn_Command);
         rBtn.CommandName = "Edit";
         Random rnd = new Random();
         rBtn.ID = "test";
         container.Controls.Add(rBtn);
     }
 
     void rBtn_Command(object sender, CommandEventArgs e)
     {
         string a = (sender as RadButton).CommandName;
     }
 }
 
 public class EditItemTemplateLinks : IBindableTemplate
 {
     ListItemType lstItemType;
     protected RadButton rBtn;
     string colname = string.Empty;
     string btnText = string.Empty;
     string cmdName = string.Empty;
     string colValue = string.Empty;
     public EditItemTemplateLinks(string cName, string bText, string bCmdName)
     {
         btnText = bText;
         bCmdName = cmdName;
         colname = cName;
     }
     public void InstantiateIn(System.Web.UI.Control container)
     {
         rBtn = new RadButton();
         rBtn.Text = "Update fire";
         rBtn.ButtonType = RadButtonType.StandardButton;
         rBtn.CommandName = "Update";
         rBtn.Command += new CommandEventHandler(rBtn_Command);
         Random rnd = new Random();
         rBtn.ID = "ssss";
          
         container.Controls.Add(rBtn);
     }
 
     void rBtn_Command(object sender, CommandEventArgs e)
     {
         string a = (sender as RadButton).CommandName;
     }
 
     public IOrderedDictionary ExtractValues(Control container)
     {
         OrderedDictionary od = new OrderedDictionary();
         return od;
     }
 }
RadGrid RadGrid1 = new RadGrid();
RadGrid1.NeedDataSource +=
new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
           RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
           RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
           RadGrid1.ID = "rgDemo";
           RadGrid1.Width = Unit.Percentage(100);
           RadGrid1.PageSize = 5;
           RadGrid1.AllowPaging = true;
           RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
           RadGrid1.AutoGenerateColumns = false;
           RadGrid1.GroupingEnabled = true;
           RadGrid1.ShowGroupPanel = true;
           RadGrid1.ShowStatusBar = true;
           RadGrid1.ClientSettings.AllowDragToGroup = true;
 
           RadGrid1.MasterTableView.PageSize = 15;
           RadGrid1.MasterTableView.DataKeyNames = new string[] { "ID" };
 
           RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template;
           RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace;
           GridTemplateColumn gtc = new GridTemplateColumn();
           gtc.ItemTemplate = new ItemTemplateLabels("Description");
           gtc.HeaderText = "Description";
           gtc.EditItemTemplate = new EditItemTemplateTextBoxes("Description");
           RadGrid1.MasterTableView.Columns.Add(gtc);
 
           GridTemplateColumn gt2 = new GridTemplateColumn();
           gt2.ItemTemplate = new ItemTemplateLabels("Name");
           gt2.HeaderText = "Name";
           gt2.EditItemTemplate = new EditItemTemplateTextBoxes("Name");
           RadGrid1.MasterTableView.Columns.Add(gt2);
 
 
           GridTemplateColumn gt3 = new GridTemplateColumn();
           gt3.ItemTemplate = new ItemTemplateLinks("Links");
           gt3.UniqueName = "EditCommandColumn";
           gt3.HeaderText = "Links";
           gt3.EditItemTemplate = new EditItemTemplateLinks("Links", "Update", "Update");
           RadGrid1.MasterTableView.Columns.Add(gt3);
 
 GridButtonColumn gBtnColumn = new GridButtonColumn();
           gBtnColumn.CommandName = "Delete";
           gBtnColumn.Text = "Delete";
           gBtnColumn.ButtonType = GridButtonColumnType.LinkButton;
           RadGrid1.MasterTableView.Columns.Add(gBtnColumn);


4 Answers, 1 is accepted

Sort by
0
Jasminder
Top achievements
Rank 1
answered on 28 Nov 2011, 02:52 PM
The issue got resolved (Was related to the id's of the Buttons of Edit and Update), but no on clicking "Save", I am getting "

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

"
I searched the error and tried setting
" RadGrid1.MasterTableView.EnableColumnsViewState = false;" for the dynamic grid, but its not working.... Any help is greatly appreciated...
0
Tsvetina
Telerik team
answered on 30 Nov 2011, 04:47 PM
Hello Jasminder,

I noticed that you have set the grid's EditFormType to Template but you are using auto-generated edit form (with the EditItemTemplates of the template columns). So, remove this line and see if the issue persists:
RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template;


Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Jasminder
Top achievements
Rank 1
answered on 01 Dec 2011, 11:01 AM
Thanks for your reply... Actually i was using Databind on the grid. Removing that, resolved the issue,...
0
Harshit
Top achievements
Rank 1
answered on 03 Jan 2013, 11:47 AM
Hi Jasminder,
I want to create custom radgrid control. Do you have any idea how we can customize the gridboundcolumn or itemtemplate or EditItemTemplate and use it in our .aspx page.
Tags
Grid
Asked by
Jasminder
Top achievements
Rank 1
Answers by
Jasminder
Top achievements
Rank 1
Tsvetina
Telerik team
Harshit
Top achievements
Rank 1
Share this question
or