aspnet-devexpress-dot-web-dot-aspxcardview-8347a497.md
Enables you to initialize custom command buttons.
Namespace : DevExpress.Web
Assembly : DevExpress.Web.v25.2.dll
NuGet Package : DevExpress.Web
public event ASPxCardViewCustomButtonEventHandler CustomButtonInitialize
Public Event CustomButtonInitialize As ASPxCardViewCustomButtonEventHandler
The CustomButtonInitialize event's data class is ASPxCardViewCustomCommandButtonEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| ButtonID | Gets the processed custom button’s identifier. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| Enabled | Gets or sets a value that specifies whether the processed custom button is enabled. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| Image | Gets the settings of an image displayed within the processed custom button. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| IsEditingCard | Gets whether a custom command button is displayed within the card currently being edited. |
| LayoutItem | Gets the command layout item which owns the processed command button. |
| RenderMode | Specifies the processed custom command button’s render mode. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| Styles | Gets the processed custom button’s style. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| Text | Gets or sets the processed custom button’s text. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| Visible | Gets or sets whether the processed custom button is visible. Inherited from ASPxGridCustomCommandButtonEventArgs. |
| VisibleIndex | Gets the visible index of a data item (row, card or record) which contains the processed custom button. Inherited from ASPxGridCustomCommandButtonEventArgs. |
The CustomButtonInitialize event is raised for each custom command button, and enables 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, card, the type of cell which contains the processed button, etc.
To initialize individual built-in command buttons (edit, new, delete, etc.), handle the ASPxCardView.CommandButtonInitialize event.
Note
If you call the GetCardValues method in the CustomButtonInitialize event handler, you should take into account the specific behavior described in the ASPxCardView.GetCardValues topic.
Note
It is impossible to change the style of the selection checkbox using the CustomButtonInitialize event.
The following example handles the CommandButtonInitialize and CustomButtonInitialize events to specify the CommandButtons and CustomCommandButtons properties. The DataRow’s VisibleIndex property and criteria (set based on field values) are used to determine button visibility.
<dx:ASPxCardView ID="ASPxCardView1" runat="server" DataSourceID="AccessDataSource1"
OnCustomButtonInitialize="ASPxCardView1_CustomButtonInitialize"
OnCommandButtonInitialize="ASPxCardView1_CommandButtonInitialize"
KeyFieldName="ProductID" AutoGenerateColumns="False">
<ClientSideEvents CustomButtonClick="function(s, e) {alert('keyValue = ' + s.GetCardKey(e.visibleIndex));}" />
<SettingsBehavior AllowFocusedCard="true" />
<Columns>
<dx:CardViewTextColumn FieldName="ProductName" />
<dx:CardViewTextColumn FieldName="UnitPrice" />
<dx:CardViewTextColumn FieldName="ProductID" ReadOnly="True">
</Columns>
<CardLayoutProperties>
<Items>
<dx:CardViewCommandLayoutItem HorizontalAlign="Right"
ShowDeleteButton="True"
ShowEditButton="True" />
<dx:CardViewColumnLayoutItem ColumnName="ProductID" />
<dx:CardViewColumnLayoutItem ColumnName="Product Name" />
<dx:CardViewColumnLayoutItem ColumnName="UnitPrice" />
</Items>
</CardLayoutProperties>
</dx:ASPxCardView>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
(ASPxCardView1.CardLayoutProperties.Items[0] as CardViewCommandLayoutItem).CustomButtons.Add(CreateCustomButton());
}
}
CardViewCustomCommandButton CreateCustomButton() {
CardViewCustomCommandButton customBtn = new CardViewCustomCommandButton();
customBtn.ID = "CustomBtn";
customBtn.Text = "Custom button";
return customBtn;
}
protected void ASPxCardView1_CommandButtonInitialize(object sender, ASPxCardViewCommandButtonEventArgs e) {
if (e.VisibleIndex == -1) return;
switch (e.ButtonType) {
case CardViewCommandButtonType.Edit:
e.Visible = EditButtonVisibleCriteria((ASPxCardView)sender, e.VisibleIndex);
break;
case CardViewCommandButtonType.Delete:
e.Visible = DeleteButtonVisibleCriteria((ASPxCardView)sender, e.VisibleIndex);
break;
}
}
private bool EditButtonVisibleCriteria(ASPxCardView grid, int visibleIndex) {
object card = grid.GetDataRow(visibleIndex);
return ((DataRow)card)["ProductName"].ToString().Contains("a");
}
private bool DeleteButtonVisibleCriteria(ASPxCardView grid, int visibleIndex) {
object card = grid.GetDataRow(visibleIndex);
return ((DataRow)card)["ProductName"].ToString().Contains("b");
}
protected void ASPxCardView1_CustomButtonInitialize(object sender, ASPxCardViewCustomCommandButtonEventArgs e) {
if (e.VisibleIndex == -1) return;
if (e.ButtonID == "CustomBtn" && e.VisibleIndex % 2 != 0)
e.Visible = DefaultBoolean.False;
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
TryCast(ASPxCardView1.CardLayoutProperties.Items(0), CardViewCommandLayoutItem).CustomButtons.Add(CreateCustomButton())
End If
End Sub
Private Function CreateCustomButton() As CardViewCustomCommandButton
Dim customBtn As New CardViewCustomCommandButton()
customBtn.ID = "CustomBtn"
customBtn.Text = "Custom button"
Return customBtn
End Function
Protected Sub ASPxCardView1_CommandButtonInitialize(ByVal sender As Object, ByVal e As ASPxCardViewCommandButtonEventArgs)
If e.VisibleIndex = -1 Then
Return
End If
Select Case e.ButtonType
Case CardViewCommandButtonType.Edit
e.Visible = EditButtonVisibleCriteria(DirectCast(sender, ASPxCardView), e.VisibleIndex)
Case CardViewCommandButtonType.Delete
e.Visible = DeleteButtonVisibleCriteria(DirectCast(sender, ASPxCardView), e.VisibleIndex)
End Select
End Sub
Private Function EditButtonVisibleCriteria(ByVal grid As ASPxCardView, ByVal visibleIndex As Integer) As Boolean
Dim card As Object = grid.GetDataRow(visibleIndex)
Return DirectCast(card, DataRow)("ProductName").ToString().Contains("a")
End Function
Private Function DeleteButtonVisibleCriteria(ByVal grid As ASPxCardView, ByVal visibleIndex As Integer) As Boolean
Dim card As Object = grid.GetDataRow(visibleIndex)
Return DirectCast(card, DataRow)("ProductName").ToString().Contains("b")
End Function
Protected Sub ASPxCardView1_CustomButtonInitialize(ByVal sender As Object, ByVal e As ASPxCardViewCustomCommandButtonEventArgs)
If e.VisibleIndex = -1 Then
Return
End If
If e.ButtonID = "CustomBtn" AndAlso e.VisibleIndex Mod 2 <> 0 Then
e.Visible = DefaultBoolean.False
End If
End Sub
See Also