Skip to content

Commit 3418837

Browse files
authored
update description (#1)
1 parent a636bc6 commit 3418837

2 files changed

Lines changed: 71 additions & 14 deletions

File tree

Readme.md

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,79 @@
1-
<!-- default badges list -->
2-
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/128543079/15.1.3%2B)
3-
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/E3028)
4-
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](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
132
<!-- run online -->
143
**[[Run Online]](https://codecentral.devexpress.com/e3028/)**
154
<!-- run online end -->
165

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+
![Buttons](buttonVisibility.png)
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.
1737
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;
1941
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))
2175
76+
## Documentation
2277
78+
* [CommandButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CommandButtonInitialize)
79+
* [CustomButtonInitialize](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CustomButtonInitialize)

buttonVisibility.png

24.2 KB
Loading

0 commit comments

Comments
 (0)