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

Control of GridTemplateColumn on PostBack

4 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Guilhem
Top achievements
Rank 1
Guilhem asked on 26 Dec 2017, 03:18 AM

Hello,

I have an issue accessing the control of a, code behind generated, GridTemplateColumn from a click event of a button outside of the RadGrid.

<telerik:RadGrid ID="MyGrid" runat="server" AutoGenerateColumns="true" AllowSorting="true" AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false" OnColumnCreated="Siblings_ColumnCreated">
    <GroupingSettings CaseSensitive="false" />
    <HeaderStyle HorizontalAlign="Center" BorderWidth="1" />
    <ClientSettings AllowColumnsReorder="false">
        <Selecting AllowRowSelect="false" />
    </ClientSettings>
    <MasterTableView AllowPaging="false" DataKeyNames="MyField" CommandItemDisplay="None" TableLayout="Fixed">
    </MasterTableView>
</telerik:RadGrid>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (MyCondition)
        {
            MyGrid.MasterTableView.EditMode = GridEditMode.Batch;
            var MyTemplateColumn = new GridTemplateColumn();
            MyTemplateColumn.UniqueName = "MyTemplateColumn";
            MyTemplateColumn.HeaderText = "MyTemplateColumn";
            MyTemplateColumn.ItemTemplate = new MyTemplate();
            MyGrid.Columns.Add(MyTemplateColumn);
        }
        MyGrid.DataSource = MyDataTable;
        MyGrid.DataBind();
    }
}
 
public partial class MyTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        var cb = new RadComboBox();
        cb.ID = "MyComboBox";
        cb.AutoPostBack = false;
        container.Controls.Add(cb);
    }
}
 
protected void MyButton_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in MyGrids.Items)
    {
        //This works, I have access to all the autogenerated columns and their values
        string MyValue = item["MyField"].Text;
        //This works, I have access to the Template column
        TableCell MyTemplateColumn = item["MyTemplateColumn"];
         
        //doesn't work => Controls.Count = 0
        var MyColumnCombobox = MyTemplateColumn.Controls[0] as RadComboBox;
        //doesn't work
        var MyRowCombobox = item.FindControl("MyTemplateColumn") as RadComboBox;
    }
}

 

Anyone can help ?

Thank you.

4 Answers, 1 is accepted

Sort by
0
Guilhem
Top achievements
Rank 1
answered on 26 Dec 2017, 03:24 AM

I made a mistake in the sample above. (But both doesn't work anyway).

Of course it's not

var MyRowCombobox = item.FindControl("MyTemplateColumn") as RadComboBox;

But

var MyRowCombobox = item.FindControl("MyComboBox") as RadComboBox;
0
Eyup
Telerik team
answered on 27 Dec 2017, 07:30 AM
Hi Guilhem,

Please make sure you are not using DataBind() method to bind the grid. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:

Declarative DataSource (DataSourceID property)
Programmatic Data Binding (NeedDataSource event, + DetailTableDataBind for hierarchy). You should set the DataSource property ONLY within these event handlers.


Here is another sample with the NeedDataSource event.

Also, if you plan to create the template columns dynamically, you will need to create the entire grid during the Page_Init event handler:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-template-columns-programmatically

And the crucial point here is that you are using Batch editing. This edit mode is quite different than other editing modes and you will need to use a specific approach to access the editing controls. This is demonstrated in the following section:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-batch-edit-mode

I hope this will prove helpful.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Guilhem
Top achievements
Rank 1
answered on 28 Dec 2017, 05:51 AM

I changed my code as below (and now it's working as I want).
I could not get to hide the additional template column when my condition is false (no permission) but I can disable it which is enough.

Thank you.

<telerik:RadGrid ID="MyGrid" runat="server" OnItemDataBound="MyGrid_ItemDataBound" OnNeedDataSource="MyGrid_NeedDataSource" AllowMultiRowSelection="true" AutoGenerateColumns="true" AllowSorting="true" AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false" OnColumnCreated="MyGrid_ColumnCreated">
    <GroupingSettings CaseSensitive="false" />
    <HeaderStyle HorizontalAlign="Center" BorderWidth="1" />
    <ClientSettings AllowColumnsReorder="false">
        <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true" />
    </ClientSettings>
    <MasterTableView AllowPaging="false" DataKeyNames="MyField" CommandItemDisplay="None" TableLayout="Fixed">
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"><HeaderStyle Width="50px" /></telerik:GridClientSelectColumn>
            <telerik:GridTemplateColumn UniqueName="MyTemplateColumn" HeaderText="MyTemplateColumn">
                <ItemTemplate>
                    <telerik:RadComboBox  runat="server" ID="MyComboBox" AutoPostBack="false"></telerik:RadComboBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 

DataTable CacheDataTable;
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CacheDataTable = MyDataTable;
    }
}
 
protected void MyGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    MyGrid.DataSource = CacheDataTable;
}
 
protected void MyButton_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in MyGrids.Items)
    {
        var MyComboBox = sibling["MyTemplateColumn"].FindControl("MyComboBox") as RadComboBox;
        string MyValue = MyComboBox.SelectedValue;
    }
}
 
protected void MyGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        if (!MyCondition)
        {
            var item = (GridDataItem) e.Item;
            (item["MyTemplateColumn"].FindControl("MyComboBox") as RadComboBox).Enabled = false;
            (item["ClientSelectColumn"].Controls[0] as CheckBox).Enabled = false;
        }
    }
}
0
Eyup
Telerik team
answered on 01 Jan 2018, 09:45 AM
Hi Guilhem, 

I'm glad the provided directions have proven helpful.
Generally, when using template columns you can take advantage of the FindControl method. For specific controls you can use their Visible property, whereas for columns you can also use the Display property instead:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Common/using-the--getitems-getcolumn-and-getcolumnsafe-methods#columns

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Guilhem
Top achievements
Rank 1
Answers by
Guilhem
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or