Dynamic radgrid with template columns and "Multiple controls with same ID" error on postback

4 posts, 0 answers
  1. Brad Divine
    Brad Divine avatar
    7 posts
    Member since:
    Mar 2009

    Posted 24 Aug 2010 Link to this post

    Hi guys,

    Having some trouble with my grid. It's in a web control, dynamically built in Page_Init(). The page loads fine the first time, but on postback I get an exception that there is already a control called 'tbTemplateControl' That name belongs to one of my GridTemplateColumns, FacilityColumn. Here's the code in my codebehind

    RadGrid rgFacilities;
    protected void Page_Load(object sender, EventArgs e)
    {
        rgFacilities = (RadGrid)PlaceHolder1.FindControl("rgFacilities");
        RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(rgFacilities, rgFacilities);
    }
     
    protected void Page_Init(object sender, EventArgs e)
    {
        CreateGrid();
    }
     
    void rgFacilities_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        rgFacilities.DataSource = table;
    }
     
    DataTable _table;
    private DataTable table
    {
        get
        {
            if (_table == null)
            {
                CreateTable();
            }
            return _table;
        }
        set
        {
            _table = value;
        }
    }
     
    public Dictionary<string, decimal> totals;
    private void CreateGrid()
    {
        rgFacilities = new RadGrid();
        rgFacilities.ID = "rgFacilities";
        rgFacilities.AutoGenerateColumns=false;
        rgFacilities.ShowFooter=true;
        rgFacilities.Width=750;
        rgFacilities.NeedDataSource += new GridNeedDataSourceEventHandler(rgFacilities_NeedDataSource);
        rgFacilities.AllowMultiRowEdit=true;
        rgFacilities.MasterTableView.TableLayout = GridTableLayout.Auto;
        rgFacilities.MasterTableView.EditMode = GridEditMode.InPlace;
         
        rgFacilities.ClientSettings.Scrolling.FrozenColumnsCount=1;
        rgFacilities.ClientSettings.Scrolling.AllowScroll=true;
        rgFacilities.ClientSettings.Scrolling.SaveScrollPosition=true;
        rgFacilities.ClientSettings.Scrolling.UseStaticHeaders = true;
     
        totals = new Dictionary<string,decimal>();
        FundColumn column = new FundColumn();
        rgFacilities.MasterTableView.Columns.Clear();
        rgFacilities.MasterTableView.Columns.Add(column);
        for (int i = 1; i < table.Columns.Count; i++)
        {
            decimal total =  new decimal();
            for (int j = 0; j < table.Rows.Count; j++)
            {
                if (table.Rows[j][i] is decimal)
                {
                    total += (decimal)table.Rows[j][i];
                }
            }
            FacilityColumn col = new FacilityColumn(total,false);
            totals[col.UniqueName] = total;
            //col.Aggregate = GridAggregateFunction.Sum;
            //col.FooterAggregateFormatString = "{0:C}";
            col.DataField = table.Columns[i].ColumnName;
            col.HeaderText = table.Columns[i].ColumnName;
            rgFacilities.MasterTableView.Columns.Add(col);
            total = 0;
        }
        for (int i = 0; i < table.Rows.Count && i < rgFacilities.PageSize; i++)
        {
            rgFacilities.EditIndexes.Add(i);
        }
        this.PlaceHolder1.Controls.Clear();
        this.PlaceHolder1.Controls.Add(rgFacilities);
    }
     
    private void CreateTable()
    {
        table = new DataTable();
        table.Columns.Add(new DataColumn("Fund", typeof(string)));
        table.Columns.Add(new DataColumn("Facility1", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility2", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility3", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility4", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility5", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility6", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility7", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility8", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility9", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility10", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility11", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility12", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility13", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility14", typeof(decimal)));
        table.Columns.Add(new DataColumn("Facility15", typeof(decimal)));
        DataRow row1 = table.NewRow();
        DataRow row2 = table.NewRow();
        DataRow row3 = table.NewRow();
        DataRow row4 = table.NewRow();
        DataRow row5 = table.NewRow();
        DataRow row6 = table.NewRow();
        DataRow row7 = table.NewRow();
        DataRow row8 = table.NewRow();
        DataRow row9 = table.NewRow();
        DataRow row10 = table.NewRow();
        DataRow row11 = table.NewRow();
        DataRow row12 = table.NewRow();
        DataRow row13 = table.NewRow();
        DataRow row14 = table.NewRow();
        DataRow row15 = table.NewRow();
        DataRow row16 = table.NewRow();
        DataRow row17 = table.NewRow();
        DataRow row18 = table.NewRow();
        DataRow row19 = table.NewRow();
        DataRow row20 = table.NewRow();
     
        row1["Fund"] = "Fund1";
        row2["Fund"] = "Fund2";
        row3["Fund"] = "Fund3";
        row4["Fund"] = "Fund4";
        row5["Fund"] = "Fund5";
        row6["Fund"] = "Fund6";
        row7["Fund"] = "Fund7";
        row8["Fund"] = "Fund8";
        row9["Fund"] = "Fund9";
        row10["Fund"] = "Fund10";
        row11["Fund"] = "Fund11";
        row12["Fund"] = "Fund12";
        row13["Fund"] = "Fund13";
        row14["Fund"] = "Fund14";
        row15["Fund"] = "Fund15";
        row16["Fund"] = "Fund16";
        row17["Fund"] = "Fund17";
        row18["Fund"] = "Fund19";
        row19["Fund"] = "Fund19";
        row20["Fund"] = "Fund20";
     
        row1[1] = 0.0d;
        row1[2] = 0.0d;
        row1[3] = 0.0d;
        row1[4] = 2.0d;
        row1[5] = 2.0d;
        row1[6] = 0.0d;
        row1[7] = 0.0d;
        row1[8] = 0.0d;
        row1[9] = 2.0d;
        row1[10] = 2.0d;
        row1[11] = 0.0d;
        row1[12] = 0.0d;
        row1[13] = 0.0d;
        row1[14] = 2.0d;
        row1[15] = 2.0d;
     
        row2[1] = 1.0d;
        row2[2] = 1.0d;
        row2[3] = 1.0d;
        row2[4] = 1.0d;
        row2[5] = 1.0d;
        row2[6] = 1.0d;
        row2[7] = 1.0d;
        row2[8] = 1.0d;
        row2[9] = 1.0d;
        row2[10] = 1.0d;
        row2[11] = 1.0d;
        row2[12] = 1.0d;
        row2[13] = 1.0d;
        row2[14] = 1.0d;
        row2[15] = 1.0d;
     
        row3[1] = 0.4d;
        row3[2] = 0.5d;
        row3[3] = 0.6d;
        row3[4] = 0.7d;
        row3[5] = 0.8d;
        row3[6] = 0.4d;
        row3[7] = 0.5d;
        row3[8] = 0.6d;
        row3[9] = 0.7d;
        row3[10] = 0.8d;
        row3[11] = 0.4d;
        row3[12] = 0.5d;
        row3[13] = 0.6d;
        row3[14] = 0.7d;
        row3[15] = 0.8d;
     
        row4[1] = 3.0d;
        row4[2] = 4.0d;
        row4[3] = 5.0d;
        row4[4] = 6.0d;
        row4[5] = 7.0d;
        row4[6] = 3.0d;
        row4[7] = 4.0d;
        row4[8] = 5.0d;
        row4[9] = 6.0d;
        row4[10] = 7.0d;
        row4[11] = 3.0d;
        row4[12] = 4.0d;
        row4[13] = 5.0d;
        row4[14] = 6.0d;
        row4[15] = 7.0d;
     
        row5[1] = 1.0d;
        row5[2] = 2.0d;
        row5[3] = 3.0d;
        row5[4] = 4.0d;
        row5[5] = 5.0d;
        row5[6] = 1.0d;
        row5[7] = 2.0d;
        row5[8] = 3.0d;
        row5[9] = 4.0d;
        row5[10] = 5.0d;
        row5[11] = 1.0d;
        row5[12] = 2.0d;
        row5[13] = 3.0d;
        row5[14] = 4.0d;
        row5[15] = 5.0d;
     
        table.Rows.Add(row1);
        table.Rows.Add(row2);
        table.Rows.Add(row3);
        table.Rows.Add(row4);
        table.Rows.Add(row5);
        table.Rows.Add(row6);
        table.Rows.Add(row7);
        table.Rows.Add(row8);
        table.Rows.Add(row9);
        table.Rows.Add(row10);
        table.Rows.Add(row11);
        table.Rows.Add(row12);
        table.Rows.Add(row13);
        table.Rows.Add(row14);
        table.Rows.Add(row15);
        table.Rows.Add(row16);
        table.Rows.Add(row17);
        table.Rows.Add(row18);
        table.Rows.Add(row19);
        table.Rows.Add(row20);
    }


    And here are my template column classes:

    public class FacilityColumn : GridTemplateColumn
            {
                public FacilityColumn()
                    : this(0, false)
                {
                }
                public FacilityColumn(decimal total, bool zeroAllocation)
                    : base()
                {
                    //this.UniqueName = RandomString(10, true);
                    HeaderTemplate = new FacilityHeaderTemplate(this);
                    ItemTemplate = new FacilityItemTemplate(this);
                    EditItemTemplate = new FacilityEditItemTemplate(this);
                    FooterTemplate = new FacilityFooterTemplate(this, total, zeroAllocation);
                }
     
                 
            }
     
            public class FacilityEditItemTemplate : ITemplate, IBindableTemplate
            {
                private FacilityColumn _col;
                OrderedDictionary _changes;
     
                private OrderedDictionary Changes
                {
                    get
                    {
                        if (_changes == null)
                            _changes = new OrderedDictionary();
     
                        return _changes;
                    }
                }
     
                public FacilityEditItemTemplate(FacilityColumn col)
                {
                    _col = col;
                }
     
                public void InstantiateIn(Control container)
                {
                    TextBox tb = new TextBox();
                    tb.ID = "tb" + _col.UniqueName;
                    tb.DataBinding += new EventHandler(tb_DataBinding);
                    tb.TextChanged += new EventHandler(tb_TextChanged);
                    tb.Width = Unit.Pixel(120);
                    container.Controls.Add(tb);
                }
     
                void tb_TextChanged(object sender, EventArgs e)
                {
                    Changes.Add("Fund|" + _col.UniqueName, (sender as TextBox).Text);
                }
     
                void tb_DataBinding(object sender, EventArgs e)
                {
                    TextBox tb = (sender as TextBox);
                    GridDataItem container = (GridDataItem)tb.NamingContainer;
                    tb.Text = ((DataRowView)container.DataItem)[_col.DataField].ToString();
                }
     
                public IOrderedDictionary ExtractValues(Control container)
                {
                    return Changes;
                }
            }
     
            public class FacilityHeaderTemplate : ITemplate
            {
                private FacilityColumn _col;
     
                public FacilityHeaderTemplate(FacilityColumn col)
                    : base()
                {
                    _col = col;
                }
     
                public void InstantiateIn(Control container)
                {
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                    container.Controls.Add(lbl);
                }
     
                void lbl_DataBinding(object sender, EventArgs e)
                {
                    (sender as Label).Text = _col.HeaderText;
                }
            }
     
            public class FacilityItemTemplate : ITemplate
            {
                private FacilityColumn _col;
                public FacilityItemTemplate(FacilityColumn col)
                {
                    _col = col;
                }
     
                public void InstantiateIn(Control container)
                {
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                    container.Controls.Add(lbl);
                }
     
                void lbl_DataBinding(object sender, EventArgs e)
                {
                    Label lbl = (sender as Label);
                    GridDataItem container = (GridDataItem)lbl.NamingContainer;
                    string val = ((DataRowView)container.DataItem)[_col.DataField].ToString();
                    if (String.IsNullOrEmpty(val))
                        val = " ";
                    lbl.Text = string.Format("{0:C}", val);
                }
            }
     
            public class FacilityFooterTemplate:ITemplate
            {
                FacilityColumn _col;
                decimal _total;
                bool _zeroAllocation;
                public FacilityFooterTemplate(FacilityColumn col, decimal total, bool zeroAllocation)
                {
                    _col = col;
                    _total = total;
                    _zeroAllocation = zeroAllocation;
                }
                #region ITemplate Members
     
                public void InstantiateIn(Control container)
                {
                    Label label = new Label();
                    RadioButtonList radioList = new RadioButtonList();
                     
                    label.ID = _col.UniqueName + "-total";
                    label.Text = _total.ToString("C");
                    radioList.ID = _col.UniqueName + "-zero";
                    radioList.Items.Add(new ListItem("yes", "1"));
                    radioList.Items.Add(new ListItem("no", "0"));
                    radioList.AutoPostBack = true;
                    if (_zeroAllocation)
                        radioList.SelectedIndex = 0;
                    else
                        radioList.SelectedIndex = 1;
                    radioList.RepeatDirection = RepeatDirection.Horizontal;
                    radioList.SelectedIndexChanged += new EventHandler(radioList_SelectedIndexChanged);
                    container.Controls.Add(label);
                    container.Controls.Add(radioList);
                }
     
                void radioList_SelectedIndexChanged(object sender, EventArgs e)
                {
                     
                }
                #endregion
            }
     
            private class ReadOnlyTemplate : ITemplate, IBindableTemplate
            {
                protected Label label;
                private string colname;
     
                public ReadOnlyTemplate(string cName)
                {
                    colname = cName;
                }
     
                #region ITemplate Members
     
                public void InstantiateIn(Control container)
                {
                    label = new Label();
                    label.ID = "templateColumnLabel";
                    label.DataBinding += label_DataBinding;
                    container.Controls.Add(label);
                }
     
                void label_DataBinding(object sender, EventArgs e)
                {
                    Label label = (Label)sender;
                    GridDataItem container = (GridDataItem)label.NamingContainer;
                    label.Text = (string)((DataRowView)container.DataItem)[colname];
                }
     
                #endregion
     
                #region IBindableTemplate Members
                OrderedDictionary _changes;
     
                private OrderedDictionary Changes
                {
                    get
                    {
                        if (_changes == null)
                            _changes = new OrderedDictionary();
     
                        return _changes;
                    }
                }
                public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
                {
                    return Changes;
                }
     
                #endregion
            }
        }

    I apologize for the length, but I think that's everything you need to use in a test project.

    So I'm creating the grid in Page_Init(), databinding in NeedDataSource(). What else can I try?

    Thanks,
    Brad
  2. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 27 Aug 2010 Link to this post

    Hello Brad,

    I have examined your code and I assume that the cause of the issue is that you clear the controls in the place holder (including the grid itself) on Page_Init:
    private void CreateGrid()
    {
        rgFacilities = new RadGrid();
        rgFacilities.ID = "rgFacilities";
        //...
     
        this.PlaceHolder1.Controls.Clear();
        this.PlaceHolder1.Controls.Add(rgFacilities);
    }

    Please try removing this line from your code and let me know whether it helps.

    All the best,
    Mira
    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
  3. Brad Divine
    Brad Divine avatar
    7 posts
    Member since:
    Mar 2009

    Posted 30 Aug 2010 Link to this post

    Unfortunately, there was no change; the page still complains about multiple controls with the same ID on postback. Any other ideas?

    Thanks,
    Brad
  4. Jay
    Jay avatar
    6 posts
    Member since:
    Aug 2010

    Posted 31 Aug 2010 Link to this post

    Hey Brad,

    I see you are adding FundColumn, ie. FundColumn column = new FundColumn();
    in grid, but I do not see code for the column itself. May be the column is problematic. Try change it with some other column.
Back to Top