GridView for ASP.NET Web Forms - How to specify the settings of built-in and custom command buttons based on a condition
This example demonstrates how to handle the grid's CommandButtonInitialize
and CustomButtonInitialize
events to specify the visibility of built-in and custom command buttons.
Follow the steps below:
-
Create the Grid View control, populate it with columns, and bind it to a data source. Add a GridViewCommandColumn and enable its ShowEditButton, ShowNewButton, and ShowDeleteButton properties to display built-in command buttons. Use the column's CustomButtons property to create a custom command button.
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="ProductID" DataSourceID="AccessDataSource1" OnCommandButtonInitialize="ASPxGridView1_CommandButtonInitialize" OnCustomButtonInitialize="ASPxGridView1_CustomButtonInitialize"> <!-- ... --> <Columns> <dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True" ShowNewButton="True" ShowDeleteButton="True"> <CustomButtons> <dx:GridViewCommandColumnCustomButton ID="btnCustom" Text="CustomButton" /> </CustomButtons> </dx:GridViewCommandColumn> <!-- ... --> </Columns> </dx:ASPxGridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb" <!-- ... -- </asp:AccessDataSource>
-
Handle the grid's server-side CommandButtonInitialize event. In the handler, use the ButtonType and Visible argument properties to identify a particular command button and specify its visibility based on a custom criteria.
protected void ASPxGridView1_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e) { if (e.VisibleIndex == -1) return; switch (e.ButtonType) { case ColumnCommandButtonType.Edit: e.Visible = EditButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex); break; case ColumnCommandButtonType.Delete: e.Visible = DeleteButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex); break; } } private bool EditButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) { // ... } private bool DeleteButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) { // ... }
-
Handle the grid's server-side CustomButtonInitialize event. In the handler, use the ButtonID and Visible argument properties to identify the custom command button and specify its visibility based on a condition.
protected void ASPxGridView1_CustomButtonInitialize(object sender, ASPxGridViewCustomButtonEventArgs e) { if (e.VisibleIndex == -1) return; if (e.ButtonID == "btnCustom" && e.VisibleIndex % 2 != 0) e.Visible = DefaultBoolean.False; }
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)