Skip to main content

ASPxCardView.AddNewCard() Method

Adds a new card.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v22.2.dll

NuGet Package: DevExpress.Web


public void AddNewCard()


The AddNewCard method switches the ASPxCardView to edit mode and allows the values of the new card to be edited. To add the new card to the underlying datasource, call the ASPxCardView.UpdateEdit (or ASPxClientCardView.UpdateEdit) method. End-users can save the changes made using the Update command.

To initialize card values in code, handle the ASPxCardView.InitNewCard event.


The following example shows how to always display the card edit form. To implement this feature, add the code below to the form page.

View Example

<head runat="server">
    <title>ASPxCardView - How to add a new card when the edit form is always visible</title>
    <form id="form1" runat="server">
        <dx:ASPxCardView ID="ASPxCardView1" runat="server" AutoGenerateColumns="False" OnBeforeGetCallbackResult="ASPxCardView1_BeforeGetCallbackResult" DataSourceID="XpoDataSource1" KeyFieldName="Oid" OnCardInserted="ASPxCardView1_CardInserted">
                <SettingsTableLayout ColumnCount="2" RowsPerPage="2" />
            <SettingsBehavior AllowFocusedCard="True" />
                <dx:CardViewTextColumn FieldName="Oid" ReadOnly="True" SortIndex="0" SortOrder="Ascending" Visible="False" VisibleIndex="0">
                <dx:CardViewTextColumn FieldName="CompanyName" VisibleIndex="2">
                <dx:CardViewTextColumn FieldName="ContactName" VisibleIndex="3">
                <dx:CardViewTextColumn FieldName="Country" VisibleIndex="5">
                    <dx:CardViewCommandLayoutItem HorizontalAlign="Right" ShowEditButton="True" ShowNewButton="True">
                    <dx:CardViewColumnLayoutItem ColumnName="Oid">
                    <dx:CardViewColumnLayoutItem ColumnName="Company Name">
                    <dx:CardViewColumnLayoutItem ColumnName="Contact Name">
                    <dx:CardViewColumnLayoutItem ColumnName="Country">
                    <dx:EditModeCommandLayoutItem HorizontalAlign="Right">
        <dx:XpoDataSource ID="XpoDataSource1" runat="server" TypeName="Customer"></dx:XpoDataSource>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Xpo;
using System.Collections;

public partial class _Default : System.Web.UI.Page
    UnitOfWork uof = XpoHelper.GetNewUnitOfWork();
    protected void Page_Init(object sender, EventArgs e) {
        XpoDataSource1.Session = uof;
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack)
    protected void ASPxCardView1_CardInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
        object newKey = null;
        if (e.AffectedRecords == 1)
            ICollection objects = uof.GetObjectsToSave();
            if (objects != null && objects.Count == 1)
                IEnumerator enumeration = objects.GetEnumerator();
                Customer obj = (Customer)enumeration.Current;
                newKey = obj.Oid;
        ASPxCardView1.FocusedCardIndex = ASPxCardView1.FindVisibleIndexByKeyValue(newKey);
    protected void ASPxCardView1_BeforeGetCallbackResult(object sender, EventArgs e) {
        if (!ASPxCardView1.IsEditing && !ASPxCardView1.IsNewCardEditing)
using System;
using DevExpress.Xpo;

public class Customer : XPObject {
    public Customer (Session session)
        : base(session) { }

    public override void AfterConstruction () {

    string _CompanyName;
    public string CompanyName {
        get { return _CompanyName; }
        set { SetPropertyValue("CompanyName", ref _CompanyName, value); }

    string _ContactName;
    public string ContactName {
        get { return _ContactName; }
        set { SetPropertyValue("ContactName", ref _ContactName, value); }

    string _Country;
    public string Country {
        get { return _Country; }
        set { SetPropertyValue("Country", ref _Country, value); }
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Metadata;

/// <summary>
/// Summary description for XpoHelper
/// </summary>
public static class XpoHelper {
    static XpoHelper () {

    public static Session GetNewSession () {
        return new Session(DataLayer);

    public static UnitOfWork GetNewUnitOfWork () {
        return new UnitOfWork(DataLayer);

    private readonly static object lockObject = new object();

    static IDataLayer fDataLayer;
    static IDataLayer DataLayer {
        get {
            if (fDataLayer == null) {
                lock (lockObject) {
                    fDataLayer = GetDataLayer();
            return fDataLayer;

    private static IDataLayer GetDataLayer () {
        XpoDefault.Session = null;

        InMemoryDataStore ds = new InMemoryDataStore();
        XPDictionary dict = new ReflectionDictionary();

        return new ThreadSafeDataLayer(dict, ds);

    static void CreateDefaultObjects () {
        using (UnitOfWork uow = GetNewUnitOfWork()) {
            Customer cust = new Customer(uow);
            cust.CompanyName = "Alfreds Futterkiste";
            cust.ContactName = "Maria Anders";
            cust.Country = "Germany";

            cust = new Customer(uow);
            cust.CompanyName = "Ana Trujillo Emparedados y helados";
            cust.ContactName = "Ana Trujillo";
            cust.Country = "Mexico";

            cust = new Customer(uow);
            cust.CompanyName = "Antonio Moreno Taquería";
            cust.ContactName = "Antonio Moreno";
            cust.Country = "Mexico";

            cust = new Customer(uow);
            cust.CompanyName = "Blondel père et fils";
            cust.ContactName = "Frédérique Citeaux";
            cust.Country = "France";

            cust = new Customer(uow);
            cust.CompanyName = "Berglunds snabbköp";
            cust.ContactName = "Christina Berglund";
            cust.Country = "Sweden";

            cust = new Customer(uow);
            cust.CompanyName = "Bottom-Dollar Markets";
            cust.ContactName = "Elizabeth Lincoln";
            cust.Country = "Canada";

See Also