Skip to content

Commit 3486135

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article how-to-access-and-control-formtemplate-buttons-radgrid (#610)
Co-authored-by: KB Bot <[email protected]>
1 parent 267c6ed commit 3486135

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: Accessing and Controlling FormTemplate Buttons in RadGrid Based on CommandName
3+
description: Learn how to access and display the Insert or Update buttons within a FormTemplate of RadGrid for ASP.NET AJAX, based on the CommandName triggered.
4+
type: how-to
5+
page_title: How to Access and Control FormTemplate Buttons in RadGrid by CommandName
6+
slug: how-to-access-and-control-formtemplate-buttons-radgrid
7+
tags: radgrid, asp.net ajax, formtemplate, commandname, insert, update, buttons
8+
res_type: kb
9+
ticketid: 1667099
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>RadGrid for ASP.NET AJAX</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>All</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
In a scenario where a RadGrid utilizes CommandItemTemplate buttons alongside a FormTemplate for inserting or updating records, it's necessary to show specific buttons based on the action the user intends to perform (e.g., Insert or Update). This article demonstrates accessing buttons within a FormTemplate based on the CommandName, such as "InitInsert" for initiating an insert action.
30+
31+
This KB article also answers the following questions:
32+
33+
- How do I make the Insert or Update button visible in RadGrid FormTemplate based on the command issued?
34+
- What is the method to access controls within a FormTemplate in RadGrid?
35+
- Can I dynamically show or hide buttons in a RadGrid FormTemplate?
36+
37+
## Solution
38+
39+
To access and manipulate the visibility of Insert or Update buttons within a FormTemplate based on the `CommandName`, use the `FindControl` method to locate these buttons. Below is a step-by-step guide implemented in VB.NET:
40+
41+
1. Handle the `ItemCommand` event of the RadGrid.
42+
2. Check if the event is triggered by an item in edit mode.
43+
3. Use the `FindControl` method to locate the Insert and Update buttons within the FormTemplate.
44+
4. Adjust the visibility of these buttons based on the `CommandName`.
45+
46+
Here is a code snippet that illustrates the process:
47+
48+
````C#
49+
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
50+
{
51+
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
52+
{
53+
GridEditableItem item = (GridEditableItem)e.Item;
54+
RadButton insertButton = (RadButton)item.FindControl("InsertButton");
55+
RadButton updateButton = (RadButton)item.FindControl("EditButton");
56+
57+
if (e.CommandName == "PerformInsert")
58+
{
59+
if (insertButton != null)
60+
insertButton.Visible = true;
61+
62+
if (updateButton != null)
63+
updateButton.Visible = false;
64+
}
65+
else if (e.CommandName == "Edit")
66+
{
67+
if (insertButton != null)
68+
insertButton.Visible = false;
69+
70+
if (updateButton != null)
71+
updateButton.Visible = true;
72+
}
73+
}
74+
System.Environment.Exit(0); /* TODO ERROR: Skipped SkippedTokensTrivia */
75+
}
76+
````
77+
````VB
78+
Protected Sub RadGrid1_ItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs)
79+
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
80+
Dim item As GridEditableItem = CType(e.Item, GridEditableItem)
81+
Dim insertButton As RadButton = CType(item.FindControl("InsertButton"), RadButton)
82+
Dim updateButton As RadButton = CType(item.FindControl("EditButton"), RadButton)
83+
84+
If e.CommandName = "PerformInsert" Then
85+
If insertButton IsNot Nothing Then
86+
insertButton.Visible = True
87+
End If
88+
If updateButton IsNot Nothing Then
89+
updateButton.Visible = False
90+
End If
91+
ElseIf e.CommandName = "Edit" Then
92+
If insertButton IsNot Nothing Then
93+
insertButton.Visible = False
94+
End If
95+
If updateButton IsNot Nothing Then
96+
updateButton.Visible = True
97+
End If
98+
End If
99+
End If
100+
End Sub
101+
````
102+
103+
Replace `"InsertButton"` and `"EditButton"` with the actual IDs of your Insert and Update buttons within the FormTemplate. This example makes the Insert button visible and the Update button hidden when the `PerformInsert` command is triggered, and vice versa for the `Edit` command.
104+
105+
## See Also
106+
107+
- [Accessing Values and Controls Server-side - RadGrid Documentation](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/accessing-values-and-controls/server-side/accessing-controls)
108+
- [How to Get Access to All Controls Inside FormTemplate of RadGrid - Telerik Forums](https://www.telerik.com/forums/how-to-get-access-to-all-controls-inside-formtempate-of-radgrid)
109+
- [Two Different Insert/Edit Forms in Same Grid - Telerik Forums](https://www.telerik.com/forums/two-different-insert-edit-forms-in-same-grid)

0 commit comments

Comments
 (0)