Accessing Controls in RadGrid
Accessing Controls of built-in columns
Grid markup
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
<ClientSettings>
<Selecting AllowRowSelect="true" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="false">
<Columns>
<telerik:GridNumericColumn UniqueName="NumericCol" DataField="Freight"></telerik:GridNumericColumn>
<telerik:GridButtonColumn UniqueName="ButtonCol" Text="Button" ButtonType="FontIconButton" />
<telerik:GridCheckBoxColumn UniqueName="CheckBoxCol" DataField="IsChecked" />
<telerik:GridDateTimeColumn UniqueName="DateTimeCol" DataField="OrderDate" />
<telerik:GridDropDownColumn UniqueName="DropDownCol" DataField="ShipName" ListTextField="ShipName" ListDataMember="ShipName" />
<telerik:GridHyperLinkColumn UniqueName="HyperLinkCol" DataNavigateUrlFields="ShipName" DataTextField="ShipName" />
<telerik:GridEditCommandColumn UniqueName="EditCommandCol" />
<telerik:GridClientSelectColumn UniqueName="ClientSelectCol" />
<telerik:GridExpandColumn UniqueName="ExpandCol" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
Code behind
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
// Controls that are only available in Edit Mode
GridEditableItem editItem = (GridEditableItem)e.Item;
RadNumericTextBox rNumericTextBox = editItem["NumericCol"].Controls[0] as RadNumericTextBox;
// Depending on the PickerType property of the Column, the GridDateColumn may render different controls, DatePicker, TimePicker or DateTimePicker
RadDatePicker rDatePicker = editItem["DateTimeCol"].Controls[0] as RadDatePicker;
RadTimePicker rTimePicker = editItem["DateTimeCol"].Controls[0] as RadTimePicker;
RadDateTimePicker rDateTimePicker = editItem["DateTimeCol"].Controls[0] as RadDateTimePicker;
//Depending on the DropDownControlType property of the Column, the GridDropDownColumn may render DropDownList or RadComboBox controls
DropDownList ddl = editItem["DropDownCol"].Controls[0] as DropDownList;
RadComboBox rComboBox = editItem["DropDownCol"].Controls[0] as RadComboBox;
CheckBox chkBox = editItem["CheckBoxCol"].Controls[0] as CheckBox;
}
else if (e.Item is GridDataItem)
{
// Controls that are only available in Display Mode (DataItem)
GridDataItem item = (GridDataItem)e.Item;
ElasticButton expandBtn = item["ExpandCol"].Controls[0] as ElasticButton;
ElasticButton editCommandButton = item["EditCommandCol"].Controls[0] as ElasticButton;
ElasticButton buttonColButton = item["ButtonCol"].Controls[0] as ElasticButton;
HyperLink hyperLink = item["HyperLinkCol"].Controls[0] as HyperLink;
CheckBox chkbox = item["ClientSelectCol"].Controls[0] as CheckBox;
CheckBox chkbox2 = item["CheckBoxCol"].Controls[0] as CheckBox;
}
}
Accessing Controls in Template Column
While accessing cells with Template columns remains the same, you could use a little different approach to get the controls in the cell. For example, to access a TextBox declared in the ItemTemplate of the column:
TableCell cell = dataItem["ColumnUniqueName"];
TextBox textBox = dataItem.FindControl("TextBoxID") as TextBox;
Accessing Controls in Edit/Insert Mode
When editing or inserting a grid item, you could access and modify the controls generated in the editable item.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editableItem = e.Item as GridEditableItem;
// execute custom logic
}
}
However, you will need to use a different approach corresponding to the chosen EditFormType.
- AutoGenerated:
TextBox textBox = editableItem["ColumnUniqueName"].Controls[0] as TextBox;
- Template:
TextBox textBox = editableItem.FindControl("TextBox1") as TextBox;
- WebUserControl:
UserControl userControl = editableItem.FindControl(GridEditFormItem.EditFormUserControlID) as UserControl;
TextBox textBox = userControl.FindControl("TextBox1") as TextBox;
InPlace EditMode is supported only for an AutoGenerated EditFormType. In this case, the editable item is of type GridDataItem or GridDataInsertItem , rather than GridEditFormItem and GridEditFormInsertItem as usual.
Accessing Controls in Batch Edit Mode
Essentially, Batch editing is a bit different from the other Edit modes. It is mainly a client-side functionality and in this sense, it would be reasonable to implement javascript approaches when accessing the individual elements generated in the cells. This can be achieved using the client-side event handlers provided by RadGrid regarding its Batch editing feature: OnBatchEditOpened Client-Side Event
Let's take for example the GridDateTime column of a RadGrid with 10 items per page. Unlike the other Edit modes, there are not 10 different RadDatePicker controls generated to edit each of the records, but there is only 1 picker loaded on the server, which contributes for ideal performance and rendering optimization. If you access the generated picker on code-behind and apply some properties, e.g. FocusedDate, the setting will be applied to all the picker elements of the column.
How to access the built-in column editor
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
GridTableView masterTable = ((RadGrid)sender).MasterTableView;
GridDateTimeColumnEditor editor = masterTable.GetBatchColumnEditor("OrderDate") as GridDateTimeColumnEditor;
RadDatePicker picker = editor.PickerControl;
picker.FocusedDate = new DateTime(1990, 3, 3);
}
How to access custom controls in a template column
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
RadComboBox combo = ((sender as RadGrid).MasterTableView.GetBatchEditorContainer("CategoryID").FindControl("rcbCategory") as RadComboBox);
combo.EnableLoadOnDemand = true;
}
Read more in the Batch editing mode article.