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

Unable to access CheckBox values from Template columns that were added dynamically

1 Answer 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 27 Sep 2010, 11:53 PM
Hey all.

I am dynamically creating the various columns in a grid.  It is a Key value column (not visible), a simple bound column, and a number of Template columns that are dynamically added.
I am able to set the data without any problem.  However, I cannot retrieve che checkboxes in a save button click handler.

Any ideas are GREATLY appreciated.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="JobTitleEditor.ascx.cs" Inherits="LiaisonApp.Controls.JobTitleEditor" %>
<div id="JobTitleEditor">
    <asp:Button ID="btn_Save" runat="server" Text="Save" onclick="btn_Save_Click" />
    <telerik:RadGrid ID="grid_Data" runat="server" AutoGenerateColumns="False" GridLines="None" OnNeedDataSource="grid_Data_NeedDataSource">
        <ClientSettings EnableRowHoverStyle="True">
            <Scrolling AllowScroll="True" ScrollHeight="550px" UseStaticHeaders="True" />
            <Resizing AllowColumnResize="True" />
        </ClientSettings>
        <MasterTableView DataKeyNames="JobTitleID">
            <RowIndicatorColumn Visible="false" />
        </MasterTableView>
        <HeaderContextMenu EnableAutoScroll="True">
        </HeaderContextMenu>
    </telerik:RadGrid>
</div>

protected void btn_Save_Click(object sender, EventArgs args)
{
    foreach (GridDataItem item in grid_Data.MasterTableView.Items)
    {
        var row = GridDataTable.Rows.Find(item.KeyValues.ToInt());
        foreach (var jobFunction in JobFunctions)
        {
            var columnName = GetJobFunctionColumnName(jobFunction.ListEnumID);
            var checkbox = (CheckBox)item.FindControl( "chk_" + jobFunction.ListEnumID.ToString());
            row[columnName] = checkbox.Checked;
        }
    }
}

private class CheckboxColumnTemplate : ITemplate
{
    int _jobFunctionID = 0;

    public void InstantiateIn(Control container)
    {
        var checkbox = new CheckBox();
        checkbox.ID = "chk_" + _jobFunctionID.ToString();
        checkbox.DataBinding += new EventHandler(Checkbox_DataBinding);
        container.Controls.Add(checkbox);
    }

    void Checkbox_DataBinding(object sender, EventArgs args)
    {
        var checkbox = sender as CheckBox;
        var dataRow = (checkbox.NamingContainer as GridDataItem).DataItem as DataRowView;
        checkbox.Checked = dataRow.Row[GetJobFunctionColumnName(_jobFunctionID)].ToBool();
    }

    public CheckboxColumnTemplate(int jobFunctionID)
    {
        this._jobFunctionID = jobFunctionID;
    }
}

private void BuildGrid()
{
    var gridColumn = new GridBoundColumn();
    gridColumn.DataField = "JobTitleID";
    gridColumn.Visible = false;
    gridColumn.UniqueName = "JobTitleID";
    grid_Data.Columns.Add(gridColumn);

    gridColumn = new GridBoundColumn();
    gridColumn.DataField = "JobTitle";
    gridColumn.HeaderText = "Job Title";
    gridColumn.HeaderStyle.Width = new Unit(180, UnitType.Pixel);
    gridColumn.FooterStyle.Width = new Unit(180, UnitType.Pixel);
    gridColumn.ItemStyle.Width = new Unit(180, UnitType.Pixel);
    grid_Data.Columns.Add(gridColumn);

    foreach (var jobFunction in JobFunctions)
    {
        var newColumn = new GridTemplateColumn();
        newColumn.HeaderText = jobFunction.StringValue;
        newColumn.ItemTemplate = new CheckboxColumnTemplate(jobFunction.ListEnumID);
        newColumn.HeaderStyle.Width = new Unit(80, UnitType.Pixel);
        newColumn.FooterStyle.Width = new Unit(80, UnitType.Pixel);
        newColumn.ItemStyle.Width = new Unit(80, UnitType.Pixel);
        newColumn.UniqueName = GetJobFunctionColumnName(jobFunction.ListEnumID);
        newColumn.DataField = newColumn.UniqueName;

        grid_Data.Columns.Add(newColumn);
    }
}

1 Answer, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 28 Sep 2010, 05:45 PM
Ok, figured it out.

I was creating the grid columnd dynamically in the Load event handler.  The ViewState was being processed BEFORE that.
I moved my column constructing code into an Init event handler and it now works!
Tags
Grid
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Share this question
or