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

Grid with dynamic columns (GridTemplateColumn) - dynamic control events not firing...

1 Answer 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 04 Oct 2010, 08:31 PM

I opened a support ticket for this already (# 354150 ) - but wanted to get the community input on this as well and to share the solution - I'm sure I won't be the first or last to ask these questions...


I am creating a RadGrid on a page where this particular RadGrid is "loosely" bound in the sense that it only serves as a visualization of a 'summary' of some data that is presented elsewhere on another grid.

The visual paradigm desired is for a left-most column that is a GridClientSelectColumn followed by 2 more bound columns, one for an ID and another for a Name.  The remaining columns are dynamic based on another piece of supporting data (Unit Types)...  so the # of columns will vary depending on data. These dynamic columns are simply built to contain a single CheckBox control. I'm using a Template Column in this case because we want the CheckBox columns to always be editable without the row being in 'Edit' mode (if I set the row to edit mode manually, the alternating row-color theming is lost and the client wants to retain it)...  What I found was that the checkbox control in a template column was editable in all cases... a good thing for this particular scenario...

From what I've read it is reccomended to not mix static/dynamic column definition so all columns are being added dynamically at runtime in the Page_Init event (this Grid is hosted on a UserControl, not a true Page)... An additional noteworthy point is that the Page contains a RadTabStrip and RadMultiPage within a RadAjaxPanel... the UserControl hosting this RadGrid is on a RadPage (2nd tab if that matters) ....

The grid is defined in the markup as follows:

<telerik:RadGrid ID="TransporterAssignmentGrid" runat="server" OnItemCreated="TransporterAssignmentGrid_ItemCreated"
                OnItemDataBound="TransporterAssignmentGrid_ItemDataBound" OnNeedDataSource="TransporterAssignmentGrid_NeedDataSource"
                Width="99%" AutoGenerateColumns="false">
    <ClientSettings>
         <Selecting AllowRowSelect="true" />
    </ClientSettings>
    <MasterTableView DataKeyNames="TransporterId">
    </MasterTableView>
</telerik:RadGrid>

 

Then in the Page_Init event there is a call to the "CreateTransporterAssignmentGridColumns" operation whose code contains the follwing:

// Create the GridClientSelectColumn (checkbox/row-selection)...
GridClientSelectColumn gridClientSelectColumn = new GridClientSelectColumn();
gridClientSelectColumn.UniqueName = "GridClientSelectColumn";
gridClientSelectColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(25);
gridClientSelectColumn.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(25);

this.TransporterAssignmentGrid.MasterTableView.Columns.Add
    (
        gridClientSelectColumn
     );

// Create the TransporterId bound column...
GridBoundColumn transporterIdGridBoundColumn = new GridBoundColumn();
transporterIdGridBoundColumn.UniqueName = "TransporterIdColumn";
transporterIdGridBoundColumn.DataField = "TransporterId";
transporterIdGridBoundColumn.DataType = typeof(Int32);
transporterIdGridBoundColumn.ReadOnly = true;
transporterIdGridBoundColumn.HeaderText = "Transporter ID";
transporterIdGridBoundColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(125);
transporterIdGridBoundColumn.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(125);
 
this.TransporterAssignmentGrid.MasterTableView.Columns.Add
    (
        transporterIdGridBoundColumn
     );

// Create the TransporterName bound column...
GridBoundColumn transporterNameGridBoundColumn = new GridBoundColumn();
transporterNameGridBoundColumn.UniqueName = "TransporterNameColumn";
transporterNameGridBoundColumn.DataField = "TransporterName"; // Handled in ItemDataBound...
transporterNameGridBoundColumn.DataType = typeof(Int32);
transporterNameGridBoundColumn.ReadOnly = true;
transporterNameGridBoundColumn.HeaderText = "Transporter Name";
transporterNameGridBoundColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(350);
transporterNameGridBoundColumn.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(350);

this.TransporterAssignmentGrid.MasterTableView.Columns.Add
    (
        transporterNameGridBoundColumn
     );

// Create a column for each UnitCategory
foreach (UnitCategory unitCategory in ZoneMaintenancePage.UnitCategoryList)
{
    GridTemplateColumn gridTemplateColumn = new GridTemplateColumn();
    gridTemplateColumn.UniqueName = unitCategory.Name + "Column";
    gridTemplateColumn.HeaderText = unitCategory.Name;
    gridTemplateColumn.HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(100);
    gridTemplateColumn.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(100);
    gridTemplateColumn.ItemTemplate = new UnitCategoryColumnTemplate
        (
            this.Page,
            unitCategory.Name
         );

    this.TransporterAssignmentGrid.MasterTableView.Columns.Add
        (
            gridTemplateColumn
         );
}

The template for the Unit Category column contains a single CheckBox control...

/// <summary>
/// 
/// </summary>
private class UnitCategoryColumnTemplate : ITemplate
{
    #region Constructor

    /// <summary>
    /// 
    /// </summary>
    /// <param name="columnName"></param>
    public UnitCategoryColumnTemplate
        (
            Page parentPage,
            string unitCategtoryName                    
         )
    {
        this.ParentPage = parentPage;
        this.UnitCategoryName = unitCategtoryName;
    }

    #endregion

    #region Properties

    /// <summary>
    /// 
    /// </summary>
    public Page ParentPage { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string UnitCategoryName { get; set; }

    #endregion

    // Control Events

    #region UnitCategoryCheckBox_CheckedChanged
      
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void UnitCategoryCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        object x = sender as CheckBox;
    }

    #endregion

    // ITemplate Operations

    #region InstantiateIn

    /// <summary>
    /// 
    /// </summary>
    /// <param name="container"></param>
    public void InstantiateIn(System.Web.UI.Control container)
    {
        try
        {
            // Determine the Transporter ID...
            GridDataItem gridDataItem = container.NamingContainer as GridDataItem;

            int transporterId = Convert.ToInt32
                (
                    gridDataItem.GetDataKeyValue("TransporterId")
                 );

            CheckBox checkBox = new CheckBox();
            checkBox.ID = "Transporter" + transporterId + "_" + UnitCategoryName + "CheckBox";
            checkBox.AutoPostBack = true;

            checkBox.CheckedChanged += new EventHandler
                (
                    UnitCategoryCheckBox_CheckedChanged
                 );

            container.Controls.Add
                (
                    checkBox
                 );
        }
        catch
        {
            throw;
        }
    }

    #endregion
}

 

  • NOTE: The constructor argument/property ParentPage is only used so that calls to RadScriptManager.GetCurrent() can have a Page reference...

The item being used for the DataSource is a very simple List<T> object where the list item is a simple object with just the TransporterId property so that the grid can use that value for a DataKey... All the data binding is handled in the ItemDataBound event by using that TransporterId and other data sources...

protected void TransporterAssignmentGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem gridDataItem = e.Item as GridDataItem;

        // Reference the TransporterId from the data-binding object...
        int transporterId = Convert.ToInt32
            (
                gridDataItem.GetDataKeyValue("TransporterId")
             );

        // Reference the Transporter for the current TransporterId...
        Transporter transporter = ZoneMaintenancePage.TransporterList.FindByTransporterId
            (
                transporterId
             );

        // Reference the ZoneTransporterAssignment entries for the current Transporter...
        TransporterZoneAssignmentList transporterZoneAssignmentList = ZoneMaintenancePage.TransporterZoneAssignmentList.FindByTransporterId
            (
                transporterId
             );

        // Populate the Transporter ID/Name columns...
        gridDataItem.Cells[gridDataItem.GetIndexOfColumn("TransporterIdColumn")].Text = transporter.Id.ToString();
        gridDataItem.Cells[gridDataItem.GetIndexOfColumn("TransporterNameColumn")].Text = transporter.Name;

        // Populate the Unit Category columns...
        foreach (UnitCategory unitCategory in ZoneMaintenancePage.UnitCategoryList)
        {
            // Reference the entry for this Unit Category (should be 1 record only if any)...
            TransporterZoneAssignmentList unitCategoryTransporterZoneAssignmentList = transporterZoneAssignmentList.FindByUnitCategoryId
                (
                    unitCategory.UnitCategoryId
                 );

            // Reference & populate the CheckBox for the appropriate GridCheckBoxColumn...
            CheckBox checkBox = gridDataItem.Cells[gridDataItem.GetIndexOfColumn(unitCategory.Name + "Column")].Controls[0] as CheckBox;
            checkBox.Checked = (unitCategoryTransporterZoneAssignmentList.Count > 0);
        }
    }
}

 

  • NOTE: Above there is a custom C# Extension Method being used by the name of 'GetIndexOfColumn'

The problem I am having is being able to use the CheckBox controls' CheckedChanged event... I can not get this Event to fire properly... I have an event wired-up in the Template control class itself and have used it to set a breakpoint and test - but I never hit my breakpoint...


Additionally,

[A] I've tried adding the EventHandler during the Grid's ItemDataBound event...

[B] I've tried adding the EventHandler during the Grid's ItemCreated event...

[C] I've tried adding RadScriptManager.GetCurrent(ParentPage).RegisterAsyncPostBackControl(checkBox) in the Template (before adding to the Controls collection)...

  

 

[D] I've tried adding RadScriptManager.GetCurrent(ParentPage).RegisterPostBackControl(checkBox) in the Template (before adding to the Controls collection)...

What I have been able to diagnose this far is that I'm getting a JavaScript error, which seems to be AJAX-related from what I've found via Google searching... I am getting the "PageRequestManagerParserErrorException" error... which is what led me to trying [C] and [D] above...

When I tried [D] above, I was able to see Page_Init/Load firing but I still get an exception - but oddly enough it's related to the GridClientSelectColumn for the current grid... The server-side exception message was "Multiple controls with the same ID 'GridClientSelectColumnSelectCheckBox' were found. FindControl requires that controls have unique IDs."

What is the appropriate approach to achieve these goals?  My desired functionality would be for the CheckedChanged event handler code to reside in the User Control itself - and not the template control... 

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 07 Oct 2010, 12:24 PM
Hello Brad,

I see that in the support ticket that you have opened you have not posted any further enquiries after you received the last answer. If there are any unanswered questions left, please, post them there, so we can continue communication based on the already discussed points.

Best wishes,
Tsvetina
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
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or