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

[Solved] RadGrid FindControl Method & Programatic Creation Of RadGrid

1 Answer 165 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 26 May 2009, 03:57 PM
Could someone please help with a slightly confusing matter?

I am creating a RadGrid programatically. When the OnClick event of a button on the same form fires, I call my own Method that loops through the .Items collection and tries to find the values stored in controls that reside in columns in the grid. These controls are added to the grid programatically as per the documentation using my own class (implementing ITemplate for the ItemTemplate property of the columns.

Initially I could not find the controls using the FindControl method. After a little investigation I found that if I define my grid programatically in either the Page_Init method or the Init method of the RadPageView that the grid is in - then I can access the controls using the FindControls method. 

However..... as the two methods above get called very frequently before the OnClick event of my button gets fired, the columns are added everytime either Init method runs. If I include a RadGrid.Columns.Count check or RadGrid.MasterTableView.Columns.Count check, the value is always 0.

Any ideas?

Thanks in advance.



EDIT: Code Samples...

Here is the template class I have;

 

public class TextBoxTemplate : ITemplate  
    {  
        protected RadNumericTextBox textBox;  
 
        public void InstantiateIn(System.Web.UI.Control container)  
        {  
            textBox = new RadNumericTextBox();  
            textBox.ID = "txtTest";  
            textBox.Skin = "Telerik";  
            textBox.Width = Unit.Pixel(100);  
            container.Controls.Add(textBox);  
        }  
    } 

Here is how I define the gris programatically;

protected void DefineGrid();  
{  
    grdData.MasterTableView.Columns.Clear();    
    grdData.Height = Unit.Pixel(300);  
    grdData.Width = Unit.Percentage(100);  
    grdData.AutoGenerateColumns = false;  
    grdData.AllowMultiRowSelection = false;  
    grdData.AllowPaging = false;  
    grdData.ClientSettings.Scrolling.AllowScroll = true;  
    grdData.MasterTableView.DataKeyNames = new string[] { "ID" };  
 
    GridTemplateColumn templateColumn = new GridTemplateColumn();  
    templateColumn.ItemTemplate = new TextBoxTemplate();  
    templateColumn.UniqueName = templateColumn.HeaderText = "Some Text Here";  
    grdData.MasterTableView.Columns.Add(templateColumn);  

Here is where the grid is defined in order to keep the required number of columns;

protected void rpv4_Init(object sender, EventArgs e)  
{  
   DefineGrid();  

These are the lines of code called in the OnClick event that will bind the data;

DefineGrid();  
grdData.DataSource = DataStoredInSessionVariable;  
grdData.DataBind();  
 

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 29 May 2009, 10:23 AM

Hello Carl,

Thank you for the explanation.

Generally, the conventions for programmatic RadGrid creation are outlined in this section of the documentation:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

I see that in your case you invoke the DefineGrid() method from within a postback event handler in addition to OnInit. This is conflict with the concepts from the help topic because the grid columns will be created for a second time too late in the page lifecycle to keep their viewstate/lifecycle consistent.

If you would like to build the grid structure programmatically on each postback and detect which control triggered the submit, consider the approach presented in this help topic (the idea is to use the Request.Form.Get(controlId) method or the Request.Form.Keys collection).

Additionally, consider using advanced binding with NeedDataSource event handling as an alternative to the simple binding with DataBind() calls.

Kind regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or