Back to Devexpress

ASPxGridView.CustomButtonInitialize Event

aspnet-devexpress-dot-web-dot-aspxgridview-80369355.md

latest8.6 KB
Original Source

ASPxGridView.CustomButtonInitialize Event

Enables you to initialize custom command buttons.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event ASPxGridViewCustomButtonEventHandler CustomButtonInitialize
vb
Public Event CustomButtonInitialize As ASPxGridViewCustomButtonEventHandler

Event Data

The CustomButtonInitialize event's data class is ASPxGridViewCustomButtonEventArgs. The following properties provide information specific to this event:

PropertyDescription
ButtonIDGets the processed custom button’s identifier. Inherited from ASPxGridCustomCommandButtonEventArgs.
CellTypeGets a value that specifies in which row a custom button is displayed.
ColumnGets a command column which owns the processed custom button.
EnabledGets or sets a value that specifies whether the processed custom button is enabled. Inherited from ASPxGridCustomCommandButtonEventArgs.
ImageGets the settings of an image displayed within the processed custom button. Inherited from ASPxGridCustomCommandButtonEventArgs.
IsEditingRowGets whether a custom button is displayed within the data row currently being edited.
RenderModeSpecifies the processed custom command button’s render mode. Inherited from ASPxGridCustomCommandButtonEventArgs.
StylesGets the processed custom button’s style. Inherited from ASPxGridCustomCommandButtonEventArgs.
TextGets or sets the processed custom button’s text. Inherited from ASPxGridCustomCommandButtonEventArgs.
VisibleGets or sets whether the processed custom button is visible. Inherited from ASPxGridCustomCommandButtonEventArgs.
VisibleIndexGets the visible index of a data item (row, card or record) which contains the processed custom button. Inherited from ASPxGridCustomCommandButtonEventArgs.

Remarks

The CustomButtonInitialize event fires for each custom command button, and allows you to initialize it. For instance, you can handle this event to hide individual buttons.

The event parameter’s properties allow you to identify the button, row, the type of a cell which contains the processed button, etc.

To initialize individual built-in command buttons (edit, new, delete, etc.), handle the ASPxGridView.CommandButtonInitialize event.

In the CustomButtonInitialize event, you can change the style of the selection check box.

Note

If you call the GetRowValues method in the CustomButtonInitialize event handler, you should take into account the specific behavior described in the ASPxGridView.GetRowValues topic.

Example

The following example demonstrates how to specify the properties of the command and custom command buttons in the CommandButtonInitialize and CustomButtonInitialize events. The visibleIndex argument property and a custom criteria determine the button visibility.

View Example: How to specify the settings of built-in and custom command buttons based on a condition

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1"
    KeyFieldName="ProductID" OnCommandButtonInitialize="ASPxGridView1_CommandButtonInitialize"
    OnCustomButtonInitialize="ASPxGridView1_CustomButtonInitialize">
    <ClientSideEvents CustomButtonClick="OnCustomButtonClick" />
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True" ShowNewButton="True" ShowDeleteButton="True">
            <CustomButtons>
                <dx:GridViewCommandColumnCustomButton ID="btnCustom" Text="CustomButton">
                </dx:GridViewCommandColumnCustomButton>
            </CustomButtons>
        </dx:GridViewCommandColumn>
        <!-- ... -->
    </Columns>
</dx:ASPxGridView>
js
function OnCustomButtonClick(s, e) {
    alert('keyValue = ' + s.GetRowKey(e.visibleIndex));
}
csharp
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;
    }
}
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;
}
// ...
private bool EditButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) {
    object row = grid.GetRow(visibleIndex);
    return ((DataRowView)row)["ProductName"].ToString().Contains("a");
}
private bool DeleteButtonVisibleCriteria(ASPxGridView grid, int visibleIndex) {
    object row = grid.GetRow(visibleIndex);
    return ((DataRowView)row)["ProductName"].ToString().Contains("b");
}
vb
Protected Sub ASPxGridView1_CommandButtonInitialize(ByVal sender As Object, ByVal e As ASPxGridViewCommandButtonEventArgs)
    If e.VisibleIndex = -1 Then
        Return
    End If

    Select Case e.ButtonType
        Case ColumnCommandButtonType.Edit
            e.Visible = EditButtonVisibleCriteria(DirectCast(sender, ASPxGridView), e.VisibleIndex)
        Case ColumnCommandButtonType.Delete
            e.Visible = DeleteButtonVisibleCriteria(DirectCast(sender, ASPxGridView), e.VisibleIndex)
    End Select
End Sub
Protected Sub ASPxGridView1_CustomButtonInitialize(ByVal sender As Object, ByVal e As ASPxGridViewCustomButtonEventArgs)
    If e.VisibleIndex = -1 Then
        Return
    End If

    If e.ButtonID = "btnCustom" AndAlso e.VisibleIndex Mod 2 <> 0 Then
        e.Visible = DefaultBoolean.False
    End If
End Sub
' ...
Private Function EditButtonVisibleCriteria(ByVal grid As ASPxGridView, ByVal visibleIndex As Integer) As Boolean
    Dim row As Object = grid.GetRow(visibleIndex)
    Return DirectCast(row, DataRowView)("ProductName").ToString().Contains("a")
End Function
Private Function DeleteButtonVisibleCriteria(ByVal grid As ASPxGridView, ByVal visibleIndex As Integer) As Boolean
    Dim row As Object = grid.GetRow(visibleIndex)
    Return DirectCast(row, DataRowView)("ProductName").ToString().Contains("b")
End Function

See Also

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace