Skip to content

Commit ca56603

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article grid-conditional-display-edit-controls-in-batch-edit (#722)
Co-authored-by: KB Bot <[email protected]>
1 parent 1c5be5d commit ca56603

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: Displaying Different Edit Column Controls Based on Column Value in RadGrid Batch Edit Mode
3+
description: Solve the problem of conditionally displaying RadComboBox or RadTextBox controls in a RadGrid column based on another column's value in batch edit mode.
4+
type: how-to
5+
page_title: Conditional Display of Edit Controls in RadGrid Batch Edit Mode
6+
meta_title: Conditional Display of Edit Controls in RadGrid Batch Edit Mode
7+
slug: grid-conditional-display-edit-controls-in-batch-edit
8+
tags: grid, ui for asp.net ajax, radgrid, edit column, batch edit, radcombobox, radtextbox
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>UI for ASP.NET AJAX Grid</td>
18+
</tr>
19+
<tr>
20+
<td>Version</td>
21+
<td>All</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
26+
## Description
27+
28+
I want to conditionally display either a [RadDropDownList ](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/overview) or a [RadTextBox](https://docs.telerik.com/devtools/aspnet-ajax/controls/textbox/overview) in the edit column of a RadGrid using batch edit mode. The control displayed should depend on the value of another column in the same row.
29+
30+
This knowledge base article also answers the following questions:
31+
- How to toggle edit controls in a RadGrid based on another column value?
32+
- How to implement conditional templates in RadGrid batch editing?
33+
- How to use client-side logic to manage edit templates in Telerik RadGrid?
34+
35+
## Solution
36+
37+
To achieve this functionality, use JavaScript and the `OnBatchEditOpening` client-side event. This event allows dynamic visibility control of the RadComboBox or RadTextBox based on the value of another column when a row is edited. Follow these steps:
38+
39+
1. Add both RadDropDownList and RadTextBox controls in the `EditItemTemplate` of the `GridTemplateColumn`.
40+
2. Use the `OnBatchEditOpening` client event to check the value of the relevant column and dynamically show or hide the appropriate control.
41+
42+
Here is an example implementation:
43+
44+
````ASP.NET
45+
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="600px" OnNeedDataSource="RadGrid1_NeedDataSource">
46+
<MasterTableView AutoGenerateColumns="False" EditMode="Batch" DataKeyNames="OrderID">
47+
<Columns>
48+
<telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32" HeaderText="OrderID" ReadOnly="True" UniqueName="OrderID"></telerik:GridBoundColumn>
49+
<telerik:GridBoundColumn DataField="AnswerTypeId" DataType="System.Int32" HeaderText="AnswerTypeId" ReadOnly="True" UniqueName="AnswerTypeId"></telerik:GridBoundColumn>
50+
<telerik:GridTemplateColumn DataField="Answer" HeaderText="Answer" UniqueName="Answer">
51+
<ItemTemplate>
52+
<asp:Label runat="server" ID="lblAnswer"><%# Eval("Answer") %></asp:Label>
53+
</ItemTemplate>
54+
<EditItemTemplate>
55+
<telerik:RadDropDownList runat="server" ID="radCboAnswer1" Width="100px">
56+
<Items>
57+
<telerik:DropDownListItem Text="Option 1" Value="1" />
58+
<telerik:DropDownListItem Text="Option 2" Value="2" />
59+
<telerik:DropDownListItem Text="Option 3" Value="3" />
60+
</Items>
61+
</telerik:RadDropDownList>
62+
<telerik:RadTextBox runat="server" ID="radTxtAnswer1" Width="250px" TextMode="MultiLine" Rows="5" />
63+
</EditItemTemplate>
64+
</telerik:GridTemplateColumn>
65+
</Columns>
66+
</MasterTableView>
67+
<ClientSettings>
68+
<ClientEvents OnBatchEditOpening="onBatchEditOpening" />
69+
</ClientSettings>
70+
</telerik:RadGrid>
71+
````
72+
73+
````Javascript
74+
function onBatchEditOpening(sender, args) {
75+
let row = args.get_row();
76+
let tableView = args.get_tableView();
77+
let answerTypeCellIndex = tableView.getColumnByUniqueName("AnswerTypeId").get_element().cellIndex;
78+
let answerTypeValue = row.cells[answerTypeCellIndex].innerText;
79+
80+
let dropDown = $telerik.findControl(document, "radCboAnswer1");
81+
let textBox = $telerik.findControl(document, "radTxtAnswer1");
82+
83+
switch (answerTypeValue) {
84+
case "Type 1":
85+
case "Type 3":
86+
case "Type 5":
87+
dropDown.set_visible(false);
88+
textBox.set_visible(true);
89+
break;
90+
case "Type 2":
91+
case "Type 4":
92+
dropDown.set_visible(true);
93+
textBox.set_visible(false);
94+
break;
95+
}
96+
}
97+
````
98+
99+
````C#
100+
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
101+
{
102+
(sender as RadGrid).DataSource = OrdersTable();
103+
}
104+
105+
private DataTable OrdersTable()
106+
{
107+
DataTable dt = new DataTable();
108+
109+
dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
110+
dt.Columns.Add(new DataColumn("Answer", typeof(string)));
111+
dt.Columns.Add(new DataColumn("AnswerTypeId", typeof(string)));
112+
113+
dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };
114+
115+
for (int i = 0; i < 5; i++)
116+
{
117+
int index = i + 1;
118+
119+
DataRow row = dt.NewRow();
120+
121+
row["OrderID"] = index;
122+
row["Answer"] = "Answer" + index;
123+
row["AnswerTypeId"] = "497" + index;
124+
125+
dt.Rows.Add(row);
126+
}
127+
128+
return dt;
129+
}
130+
````
131+
132+
This implementation ensures that only the relevant control is visible during editing based on the value of another column.
133+
134+
## See Also
135+
136+
- [OnBatchEditOpening](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/client-side-programming/events/onbatcheditopening)
137+
- [RadComboBox Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview)
138+
- [RadTextBox Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/textbox/overview)

0 commit comments

Comments
 (0)