|
| 1 | +--- |
| 2 | +title: Use RadButton for FormTemplate Inserts/Updates in RadGrid CommandItemTemplate |
| 3 | +description: Learn how to use a RadButton within the CommandItemTemplate of a RadGrid to initiate FormTemplate inserts or updates when the grid is in PopUp EditMode. |
| 4 | +type: how-to |
| 5 | +page_title: Use RadButton for FormTemplate Inserts/Updates in RadGrid CommandItemTemplate |
| 6 | +slug: grid-use-button-for-formtemplate-insert-update |
| 7 | +tags: radgrid, asp.net ajax, commanditemtemplate, editmode, popup, formtemplate, radbutton |
| 8 | +res_type: kb |
| 9 | +ticketid: 1666940 |
| 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 | +I am using a `CommandItemTemplate` tag in my RadGrid. My `MasterTableView` is set to `EditMode="PopUp"`. How can I use a RadButton to fire the FormTemplate that I have created for Inserts/Updates? This KB article also answers the following questions: |
| 30 | + |
| 31 | +- How to initiate pop-up editing in RadGrid with a button click? |
| 32 | +- How to configure RadButton to open FormTemplate for new records in RadGrid? |
| 33 | +- How to use CommandItemTemplate for initiating inserts or updates in RadGrid? |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +To use a RadButton within the `CommandItemTemplate` to trigger the FormTemplate for Inserts/Updates in a RadGrid set to `EditMode="PopUp"`, follow these steps: |
| 38 | + |
| 39 | +1. Add a RadButton inside the `CommandItemTemplate` and set the `CommandName` property of the RadButton to specify the command, such as `InitInsert` for inserting a new record. |
| 40 | + |
| 41 | +Here is a sample code snippet demonstrating this: |
| 42 | + |
| 43 | +````ASP.NET |
| 44 | +<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowPaging="True" OnNeedDataSource="RadGrid1_NeedDataSource"> |
| 45 | + <MasterTableView EditMode="PopUp" DataKeyNames="OrderID" CommandItemDisplay="Top"> |
| 46 | + <CommandItemTemplate> |
| 47 | + <telerik:RadButton ID="RadButton1" runat="server" Text="Add New Record" CommandName="InitInsert" /> |
| 48 | + </CommandItemTemplate> |
| 49 | + <Columns> |
| 50 | + </Columns> |
| 51 | + <EditFormSettings EditFormType="Template" PopUpSettings-Width="600px" PopUpSettings-Height="400px"> |
| 52 | + <FormTemplate> |
| 53 | + <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> |
| 54 | + <telerik:RadButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" /> |
| 55 | + <telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" /> |
| 56 | + </FormTemplate> |
| 57 | + </EditFormSettings> |
| 58 | + </MasterTableView> |
| 59 | +</telerik:RadGrid> |
| 60 | +```` |
| 61 | + |
| 62 | +And the `NeedDataSource` event handler: |
| 63 | + |
| 64 | +````C# |
| 65 | +protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) |
| 66 | +{ |
| 67 | + (sender as RadGrid).DataSource = OrdersTable(); |
| 68 | +} |
| 69 | + |
| 70 | +private DataTable OrdersTable() |
| 71 | +{ |
| 72 | + DataTable dt = new DataTable(); |
| 73 | + // DataTable initialization and population logic... |
| 74 | + return dt; |
| 75 | +} |
| 76 | +```` |
| 77 | + |
| 78 | +### Key Components: |
| 79 | +- **CommandItemTemplate**: Contains the RadButton with `CommandName="InitInsert"`, which triggers the insert form. |
| 80 | +- **EditFormSettings**: Specifies the form template for inserts/updates in a pop-up. |
| 81 | +- **FormTemplate**: Contains controls for the pop-up editing or inserting records. |
| 82 | + |
| 83 | +Clicking the RadButton triggers the `InitInsert` command, opening the pop-up with the form template defined in the `EditFormSettings`. |
| 84 | + |
| 85 | +## See Also |
| 86 | + |
| 87 | +- [RadGrid CommandItemTemplate Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/commanditem/command-item-template) |
| 88 | +- [RadGrid Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/overview) |
0 commit comments