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;
Here is how I define the gris programatically;
Here is where the grid is defined in order to keep the required number of columns;
These are the lines of code called in the OnClick event that will bind the data;
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(); |