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

problem while dynamically creating columns in radgrid

11 Answers 547 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Imthiyas
Top achievements
Rank 1
Imthiyas asked on 18 Mar 2009, 06:33 AM
Hai All,

       I have created radgrid and binded with dynamic columns from a datatable. I created even the edit column dynamically.But while clicking on edit i am getting this error."An Error has Occured because a control with id 'rgschedulegrid$ct100$ct104$EditButton' Could not be located or a different controls is assigned to the same ID after postback .If the ID is not assigned ,explicitly set the ID property of the controls that raise postback events to avoid this error"


Thanks & Regards,
Imthiyas Ahamed.A

11 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 20 Mar 2009, 02:34 PM
Hello,

Can you please provide more details about how and in which point of page's lifecycle RadGrid is created, some code snippets will be appreciated too. Also please refer to this help topics on how to create RadGrid programmatically.

Sincerely yours,
Rosen
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
macy
Top achievements
Rank 1
answered on 25 Feb 2010, 07:36 AM
Hi
I have the same problem. Did you have a solution?
Regrads
Markus
0
rei
Top achievements
Rank 1
answered on 13 May 2010, 08:59 AM
Hi,

I also encountered the same problem. My scenario is this, I have a table which contains the structure of the tables that I want to load dynamically. The loading of the grid is fine, but when I clicked on add/edit/delete column, I have the same problem  "Could not be located or a different controls is assigned to the same ID after postback .If the ID is not assigned ,explicitly set the ID property of the controls that raise postback events ..".  How do we handle insert/update and delete commands on dynamically created columns?


Thanks
0
Pavlina
Telerik team
answered on 14 May 2010, 02:58 PM
Hello,

I have prepared a simple working project in order to reproduce the behavior you are describing but to no avail. I am attaching the page for your reference. Please let me know if you manage to reproduce what you are getting in this project and what steps to follow, so that we can test it too.

Regards,
Pavlina
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
Amit
Top achievements
Rank 1
answered on 16 Nov 2010, 08:34 PM

I still have the same problem. I am programmatically trying to create a RadGrid columns based on a configuration file. The grid display the columns and rows fine but I get an error when I click on the update column. Here is the code that creates the columns in code behind
--------------------------------------------------------------------------
this.RadGrid1.MasterTableView.Columns.Clear();
GridBoundColumn boundColumn;

boundColumn = new GridBoundColumn();
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = "UserID;
boundColumn.HeaderText = "UserID;

boundColumn = new GridBoundColumn();
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = "UserName;
boundColumn.HeaderText = "UserName;

GridEditCommandColumn editColumn = new GridEditCommandColumn();
editColumn.ButtonType = GridButtonColumnType.ImageButton;
editColumn.UniqueName = "EditCommandColumn";
this.RadGrid1.MasterTableView.Columns.Add(editColumn);

this.RadGrid1.MasterTableView.DataKeyNames = new string[1] { "UserID" };
-----------------------------------------------------------------------------------------

Here is the code for the relevant part of the aspx page
-----------------------------------------------------------------------
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="True" AutoGenerateColumns="False" OnUpdateCommand="RadGrid1_UpdateCommand">
       <MasterTableView AllowMultiColumnSorting="true" GroupLoadMode="Client" CommandItemDisplay="Top" EditMode="InPlace">
            <EditFormSettings>
                      <EditColumn UniqueName="EditCommandColumn" ButtonType="ImageButton" />
            </EditFormSettings>
       </MasterTableView>        
</telerik:RadGrid>
-----------------------------------------------------------------------

but now when I click on the update button I get this error

"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An error has occurred because a control with id 'ctl00$contentbody$RadGrid1$ctl00$ctl04$EditButton' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error."

Do you have any sample code that I can look at to see how to update/add/delete rows in radgrid programmatically. Please assist.

Thank you.


0
rei
Top achievements
Rank 1
answered on 16 Nov 2010, 10:53 PM
Hi,

I was able to make it work but by also creating the Grid programmatically  and doing it at the Page_Init event.
0
Pavlina
Telerik team
answered on 17 Nov 2010, 03:38 PM
Hello,

Please have in mind that columns should be added to the corresponding collection first, before the values for their properties are set. To avoid this error you should change your code as shown below:
C#:
this.RadGrid1.MasterTableView.Columns.Clear(); 
GridBoundColumn boundColumn; 
    
boundColumn = new GridBoundColumn(); 
this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
boundColumn.DataField = "UserID; 
boundColumn.HeaderText = "UserID; 
    
boundColumn = new GridBoundColumn(); 
this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
boundColumn.DataField = "UserName; 
boundColumn.HeaderText = "UserName; 
    
GridEditCommandColumn editColumn = new GridEditCommandColumn(); 
this.RadGrid1.MasterTableView.Columns.Add(editColumn); 
editColumn.ButtonType = GridButtonColumnType.ImageButton; 
editColumn.UniqueName = "EditCommandColumn"
    
this.RadGrid1.MasterTableView.DataKeyNames = new string[1] { "UserID" };

You can review this help article for more information.

All the best,
Pavlina
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Chandan Dey
Top achievements
Rank 1
answered on 05 Dec 2011, 03:36 PM
Hi Pavlina,

I Have faced a problem with my radgrid. My requirement  I have a column which is was showing its cell value dynamically in some condition its show a radbutton and in other condition its show a lable value but the column type is GridBoundColumn. Everything is working fine but when I do selection changed from outside radcombobox to filter data in radgrid the error :
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An error has occurred because a control with id 'ctl00$ContentPlaceHolder1$rgOrderDetails$ctl00$ctl04$ctl02' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

I have populate radgrid from my codebehind with rgOrderDetail.Datasource = dtSet; 
And set column cell (either its RadButton/Lable) in rgOrderDetail_ItemDataBound. The code is :

protected void rgOrderDetails_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Item || e.Item.ItemType == Telerik.Web.UI.GridItemType.AlternatingItem)
            {
                string cellText = string.Empty;
                foreach (TableCell cell in e.Item.Cells)
                {
                    if (e.Item.Cells.GetCellIndex(cell) == 8)
                    {
                        if (cell.Text.Trim() == null || cell.Text.Trim() == string.Empty || cell.Text.Trim() == "&nbsp;" || cell.Text.Trim() == "I")
                        {
                            RadButton btnComplete = new RadButton();
                            //cell.Controls.Add(btnComplete);
                            btnComplete.Width = Unit.Pixel(101);
                            btnComplete.Height = Unit.Pixel(29);
                            btnComplete.Text = "Complete";
                            btnComplete.CssClass = "complete_button";
                            btnComplete.HoveredCssClass = "goButtonClassHov";
                            btnComplete.Image.ImageUrl = "Images/cb_empty_Green.png";
                            btnComplete.Image.IsBackgroundImage = true;
                            btnComplete.CommandName = "Complete";
                            btnComplete.Attributes.Clear();

                            cell.Controls.Add(btnComplete);
                            cellText = string.Empty;
                        }
                        else
                        {
                            Label lblRecommendText = new Label();
                            lblRecommendText.Text = "Completed";
                            lblRecommendText.BackColor = System.Drawing.Color.Transparent;
                            cell.Controls.Add(lblRecommendText);
                        }
                    }
                }
            }
        }

And another thing I have an another grid with same logic is fire but the sorting not properly. Is there is any data problem. to sort this column.

I am sending my screen sort.

Thanks.
Chandan
0
Andrey
Telerik team
answered on 06 Dec 2011, 02:16 PM
Hello Chandan,

This error is mostly caused by broken ViewState due to the dynamic creation of some control. As this article describes you need to recreate the controls on every page postback, not only on initial load.

Apply these suggestions on your project and check whether the issue is resolve.

In order to help I need further information about the sorting problem, please elaborate a bit more what is the exact problem, when is happening what is causing it and similar information.

All the best,
Andrey
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
Chandan Dey
Top achievements
Rank 1
answered on 19 Dec 2011, 05:58 PM
Hi Andrey,

Thanks for your reply but sorry to give you late answer.

I've tried with your suggestion but I can not understand the approach.

My requirment :
I have RadGrid where data is binding from page load.
And in RadGrid ItemDataBound there is some manupulation in a column called Status I was add some cells are Button and some cells are label.
And filter the data on that column with a RadCombobox Items (All/Complete/Incomplete).
Everything is fine accept Items (All/Incomplete) working fine but when I select "Complete" there is a javascript error and page is break and filter not happning.

The code is like this:
protected void Page_Load(object sender, EventArgs e)
{
       rgOrderDetails.DataSource = serviceOrder.LoadData(connection);
       rgOrderDetails.DataBind();
}
protected void rgOrderDetails_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item.Cells.GetCellIndex(cell) == 8)
        {
               if (cell.Text.Trim() == null || cell.Text.Trim() == string.Empty || cell.Text.Trim() == "&nbsp;" || cell.Text.Trim() == "I")
               {
                     RadButton btnComplete = new RadButton();
                     cell.Controls.Add(btnComplete);
                     btnComplete.Width = Unit.Pixel(101);
                     btnComplete.Height = Unit.Pixel(29);
                     btnComplete.Text = "Complete";
                     btnComplete.CssClass = "complete_button";
                     btnComplete.HoveredCssClass = "goButtonClassHov";
                     btnComplete.Image.ImageUrl = "Images/cb_empty_Green.png";
                     btnComplete.Image.IsBackgroundImage = true;
                     btnComplete.CommandName = "Complete";
                     btnComplete.Attributes.Clear();
               }
               else
               {
                      Label lblRecommendText = new Label();
                      lblRecommendText.Text = "Completed";
                      lblRecommendText.BackColor = System.Drawing.Color.Transparent;
                      cell.Controls.Add(lblRecommendText);
               }
        }
}
protected void cmbStatus_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
       rgOrderDetails.DataSource = serviceOrder.LoadData(connection);
       rgOrderDetails.DataBind();
}

Error is :
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An error has occurred because a control with id 'ctl00$ContentPlaceHolder1$rgOrderDetails$ctl00$ctl04$ctl02' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

I will send you the screen shot of my page also.

Thnaks
Chandan
0
Andrey
Telerik team
answered on 20 Dec 2011, 02:00 PM
Hi Chandan,

Please modify your code in compliance with this help article. As this help article says you should use advanced data binding when you are using advanced features or RadGrid like paging, filtering,sorting and so on.

Additionally, you could check this help article for an overview of what you need to know when you are manipulating the structure of RadGrid dynamically. Basically it is always better to add controls statically and then just change their properties in order to fit your needs. Like if you want some control only to appear in a response to some action it is better to change its visible property when this action is performed rather than add it and remove it every time. Very useful article  about dynamic controls could be found in the Infinities loop blog.

So, in conclusion it is better to move out your logic for adding buttons to RadGrid from the ItemDataBound event to some other event(for example Page_Load or Page_Init) or to declare it statically in ASPX file. The error you are receiving is most probably caused by this line of code:

cell.Controls.Add(btnComplete);

This statement is executed on every Page reload and because it is trying to add an already added control to the page the framework is raising the exception.

Apply these suggestion to your project and check whether the issue is resolved.

Kind regards,
Andrey
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
Tags
Grid
Asked by
Imthiyas
Top achievements
Rank 1
Answers by
Rosen
Telerik team
macy
Top achievements
Rank 1
rei
Top achievements
Rank 1
Pavlina
Telerik team
Amit
Top achievements
Rank 1
Chandan Dey
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or