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

RadGrid TemplateField Sorting in not working

5 Answers 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tilak
Top achievements
Rank 1
Tilak asked on 20 Aug 2009, 05:48 AM

I have a template definition as below:

public class BoundTemplate : System.Web.UI.ITemplate  
    {  
        public ListItemType templateType;  
        public string columnName, dataType, timeZone, sortExpression;  
        public bool convertTZ;  
        public event CommandEventHandler OnSorting;  
 
        public BoundTemplate(ListItemType type, string colName, string sortExpression)  
        {  
            templateType = type;  
            this.columnName = colName;  
            this.sortExpression = sortExpression;  
        }  
 
        public BoundTemplate(ListItemType type, string colName, CommandEventHandler sortHandler, string sortExpression)  
        {  
            templateType = type;  
            this.columnName = colName;  
            this.OnSorting = sortHandler;  
            this.sortExpression = sortExpression;  
        }  
 
        public BoundTemplate(ListItemType type, string colName, string dataType, string timeZone, bool convertTZ)  
        {  
            templateType = type;  
            this.columnName = colName;  
            this.dataType = dataType;  
            this.timeZone = timeZone;  
            this.convertTZ = convertTZ;  
        }  
 
 
        public void InstantiateIn(Control container)  
        {  
            switch (templateType)  
            {  
                case ListItemType.Header:  
                    if (OnSorting != null)  
                    {  
                        LinkButton lb = new LinkButton();  
                        lb.Text = columnName;  
                        //lb.CommandName = "Sort";  
                        lb.Command += new CommandEventHandler(lb_Command);  
                        lb.CommandArgument = sortExpression;  
                        container.Controls.Add(lb);  
                    }  
                    else 
                    {  
                        Label lbl = new Label();  
                        lbl.Text = columnName;  
                        container.Controls.Add(lbl);  
                    }  
 
                    break;  
                case ListItemType.Item:  
                case ListItemType.AlternatingItem:  
                    Label dataLabel = new Label();  
                    dataLabel.Text = columnName;  
                    container.Controls.Add(dataLabel);  
                    dataLabel.DataBinding += new EventHandler(Item_DataBinding);  
                    break;  
 
                case ListItemType.Footer:  
                    break;  
            }  
        }  
 
        public void lb_Command(object sender, CommandEventArgs e)  
        {  
            if (OnSorting == null)  
                throw new Exception("Sort handler is not hooked up.");  
 
            OnSorting(sender, e);  
        }  
 

In the code behind I add the control to the grid dynamically as below:

 

GridTemplateColumn tfield = new GridTemplateColumn();  
                          
BoundTemplate headerTemplate = new BoundTemplate(ListItemType.Header, displayName, fieldName);  
if (field.SelectSingleNode("@sortable") != null && field.SelectSingleNode("@sortable").Value == "true")  
headerTemplate.OnSorting += new CommandEventHandler(SortGrid4TemplateFields);  
tfield.HeaderTemplate = headerTemplate;  
tfield.ItemTemplate = new BoundTemplate(ListItemType.Item, columnName, dataType, timeZone, convertTZ);  
tfield.ItemStyle.HorizontalAlign = SetAlignment(alignment);  
tfield.DataField = columnName;  
tfield.UniqueName = fieldName;  
//tfield.DataType = Type.GetType(dataType);  
tfield.SortExpression = fieldName;  
//tfield.FilterListOptions = GridFilterListOptions.VaryByDataType;  
 
RadGrid1.MasterTableView.Columns.Add(tfield); 

 

And finally, i have set the grid properties as below:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="true" AllowSorting="true" AllowPaging="true" PageSize="10" OnSortCommand="CustomSort">          
<MasterTableView AutoGenerateColumns="false" AllowCustomSorting="true"></MasterTableView>   </telerik:RadGrid> 


The issue is the OnSorting event handler is never getting invoked, nor is the CustomSort handler. I am trying to integrate the RadGrid control into our existing grid which has like ten templates defined and this is one of them

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Aug 2009, 06:11 AM
Hi Tilak,

From the above post I can see that you are trying to dynamically add template columns to the column collection of a statically declared Grid. So while adding columns to a statically declared Grid  the columns  should be added to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.

 You may refer the following help article for getting more details regarding Programmatic creation.
Programmatic creation

Shinu
0
Tilak
Top achievements
Rank 1
answered on 21 Aug 2009, 03:55 PM
This happens when the filtering is on. Once I turn off the filtering everything works fine. Now when I turn on the filtering it gives an error Multiple controls with the same controlID 'FilterTextBox....' are found, exception while FindControl. Any ideas how I can get an handle on Filter item create or what is going on here
0
Greg
Top achievements
Rank 2
answered on 23 Aug 2009, 08:41 PM
In my experience, for whatever that's worth, the only way to avoid this is to create the grid in page_init, with a placeholder.  Make sure you do all of your additions to the grid before placing the grid in the placeholder.  Trying to add a template column to a grid created in page_load, or statically created, or trying to add it after adding it to the placeholder in page_init, causes all sorts of issues, that has made my head hurt on many occasions.

Kuba
0
Jacob Ellis
Top achievements
Rank 1
answered on 24 Aug 2011, 10:27 PM
I am experiencing the exact same issue (same setup, programmatic creation with custom template columns, sort not triggering event, load in page_init). Were you ever able to resolve this?
0
Vasil
Telerik team
answered on 30 Aug 2011, 08:55 AM
Hi Jacob Ellis,

When you load the grid on Page_Init you should attach the handler every time. Also make sure that the view state of the grid is enabled.
See this help topic about programmatic creation and this demo with grid created on Page_Init.

Greetings,
Vasil
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Tilak
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tilak
Top achievements
Rank 1
Greg
Top achievements
Rank 2
Jacob Ellis
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or