|
1 | | -<!-- default badges list --> |
2 | | - |
3 | | -[](https://supportcenter.devexpress.com/ticket/details/E3028) |
4 | | -[](https://docs.devexpress.com/GeneralInformation/403183) |
5 | | -<!-- default badges end --> |
6 | | -<!-- default file list --> |
7 | | -*Files to look at*: |
8 | | - |
9 | | -* [Default.aspx](./CS/WebSite/Default.aspx) (VB: [Default.aspx](./VB/WebSite/Default.aspx)) |
10 | | -* [Default.aspx.cs](./CS/WebSite/Default.aspx.cs) (VB: [Default.aspx.vb](./VB/WebSite/Default.aspx.vb)) |
11 | | -<!-- default file list end --> |
12 | | -# How to specify built-in and Custom Command Buttons’ properties based on custom criteria |
| 1 | +# GridView for ASP.NET Web Forms - How to specify the settings of built-in and custom command buttons based on a condition |
13 | 2 | <!-- run online --> |
14 | 3 | **[[Run Online]](https://codecentral.devexpress.com/e3028/)** |
15 | 4 | <!-- run online end --> |
16 | 5 |
|
| 6 | +This example demonstrates how to handle the grid's `CommandButtonInitialize` and `CustomButtonInitialize` events to specify the visibility of built-in and custom command buttons. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Follow the steps below: |
| 13 | + |
| 14 | +1. Create the [Grid View](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView) control, populate it with columns, and bind it to a data source. Add a [GridViewCommandColumn](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn) and enable its [ShowEditButton](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn.ShowEditButton), [ShowNewButton](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn.ShowNewButton), and [ShowDeleteButton](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn.ShowDeleteButton) properties to display built-in command buttons. Use the column's [CustomButtons](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn.CustomButtons) property to create a custom command button. |
| 15 | + |
| 16 | + ```aspx |
| 17 | + <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="ProductID" |
| 18 | + DataSourceID="AccessDataSource1" OnCommandButtonInitialize="ASPxGridView1_CommandButtonInitialize" |
| 19 | + OnCustomButtonInitialize="ASPxGridView1_CustomButtonInitialize"> |
| 20 | + <!-- ... --> |
| 21 | + <Columns> |
| 22 | + <dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True" ShowNewButton="True" |
| 23 | + ShowDeleteButton="True"> |
| 24 | + <CustomButtons> |
| 25 | + <dx:GridViewCommandColumnCustomButton ID="btnCustom" Text="CustomButton" /> |
| 26 | + </CustomButtons> |
| 27 | + </dx:GridViewCommandColumn> |
| 28 | + <!-- ... --> |
| 29 | + </Columns> |
| 30 | + </dx:ASPxGridView> |
| 31 | + <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb" |
| 32 | + <!-- ... -- |
| 33 | + </asp:AccessDataSource> |
| 34 | + ``` |
| 35 | +
|
| 36 | +2. Handle the grid's server-side [CommandButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CommandButtonInitialize) event. In the handler, use the [ButtonType](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridViewCommandButtonEventArgs.ButtonType) and [Visible](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridCommandButtonEventArgs.Visible) argument properties to identify a particular command button and specify its visibility based on a custom criteria. |
17 | 37 |
|
18 | | -<p>The example demonstrates how to specify the CommandButtons and Custom CommandButtons properties by handling the CommandButtonInitialize and CustomButtonInitialize events. The DataRows' VisibleIndex property and criteria set based on field values are used to determine the buttons' visibility.</p> |
| 38 | + ```csharp |
| 39 | + protected void ASPxGridView1_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e) { |
| 40 | + if (e.VisibleIndex == -1) return; |
19 | 41 |
|
20 | | -<br/> |
| 42 | + switch (e.ButtonType) { |
| 43 | + case ColumnCommandButtonType.Edit: |
| 44 | + e.Visible = EditButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex); |
| 45 | + break; |
| 46 | + case ColumnCommandButtonType.Delete: |
| 47 | + e.Visible = DeleteButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex); |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + private bool EditButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) { |
| 53 | + // ... |
| 54 | + } |
| 55 | + private bool DeleteButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) { |
| 56 | + // ... |
| 57 | + } |
| 58 | + ``` |
| 59 | +
|
| 60 | +3. Handle the grid's server-side [CustomButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CustomButtonInitialize) event. In the handler, use the [ButtonID](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridCustomCommandButtonEventArgs.ButtonID) and [Visible](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridCustomCommandButtonEventArgs.Visible) argument properties to identify the custom command button and specify its visibility based on a condition. |
| 61 | +
|
| 62 | + ```csharp |
| 63 | + protected void ASPxGridView1_CustomButtonInitialize(object sender, ASPxGridViewCustomButtonEventArgs e) { |
| 64 | + if (e.VisibleIndex == -1) return; |
| 65 | +
|
| 66 | + if (e.ButtonID == "btnCustom" && e.VisibleIndex % 2 != 0) |
| 67 | + e.Visible = DefaultBoolean.False; |
| 68 | + } |
| 69 | + ``` |
| 70 | +
|
| 71 | +## Files to Review |
| 72 | +
|
| 73 | +* [Default.aspx](./CS/WebSite/Default.aspx) (VB: [Default.aspx](./VB/WebSite/Default.aspx)) |
| 74 | +* [Default.aspx.cs](./CS/WebSite/Default.aspx.cs) (VB: [Default.aspx.vb](./VB/WebSite/Default.aspx.vb)) |
21 | 75 |
|
| 76 | +## Documentation |
22 | 77 |
|
| 78 | +* [CommandButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CommandButtonInitialize) |
| 79 | +* [CustomButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CustomButtonInitialize) |
0 commit comments