Skip to main content

ASPxSpreadsheet.InitializeDocument Event

Occurs before a document is loaded. Handle this event to set initial document settings.

Namespace: DevExpress.Web.ASPxSpreadsheet

Assembly: DevExpress.Web.ASPxSpreadsheet.v24.2.dll

Declaration

public static event InitializeDocumentEventHandler InitializeDocument

Event Data

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

Property Description
Document Provides access to a workbook loaded into a Spreadsheet.

Remarks

The InitializeDocument event allows you to make the necessary adjustments in the document model while the document is being opened for the first time.

When a user opens a document for the first time, the document is loaded into the server memory. This action can be handled with the static InitializeDocument event handler in the Global.asax file:

void Application_Start(object sender, EventArgs e) {  
    ASPxSpreadsheet.InitializeDocument += ASPxSpreadsheet_InitializeDocument;  
}  
protected static void ASPxSpreadsheet_InitializeDocument(object sender, DevExpress.Web.ASPxSpreadsheet.SpreadsheetInitializeDocumentEventArgs e) {  
    e.Document.DocumentLoaded += Document_DocumentLoaded;  
}  
private static void Document_DocumentLoaded(object sender, EventArgs e) {  
    //....  
}  

When another user opens the same document, that document does not load again, but rather opens from server memory. InitializeDocument and DocumentLoaded events do not fire in this case.

Note that certain events are not implemented at the Spreadsheet instance level; for instance, the DocumentLoaded event. To handle such events, implement your own document management approach. Refer to the following demo for an example: Custom Document Management.

Example

The sample demonstrates how to handle the Spreadsheet’s ASPxSpreadsheet.InitializeDocument event to password protect the loaded document by specifying its password in code.

<dx:ASPxSpreadsheet ID="ASPxSpreadsheet1" runat="server"
    ClientInstanceName="spreadsheet" RibbonMode="Ribbon"
    ActiveTabIndex="0" WorkDirectory="~\App_Data\WorkDirectory\"
    ShowConfirmOnLosingChanges="False" Height="850px" Width="100%">
    <SettingsDocumentSelector>
        <FileListSettings View="Details"></FileListSettings>
    </SettingsDocumentSelector>
</dx:ASPxSpreadsheet>
using DevExpress.Spreadsheet;
using DevExpress.Web.ASPxSpreadsheet;
using DevExpress.Web.Office;

// ...
    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack)
            ReopenDocument();
    }

    protected void ReopenDocument() {
        ASPxSpreadsheet.InitializeDocument += ASPxSpreadsheet_InitializeDocument;
        string filePath = Server.MapPath("~/App_Data/WorkDirectory/AutoFilter.xlsx");
        DocumentManager.CloseDocument(filePath);
        ASPxSpreadsheet1.Open(filePath);
    }

    protected static void ASPxSpreadsheet_InitializeDocument(object sender, SpreadsheetInitializeDocumentEventArgs e) {
        e.Document.EncryptedFilePasswordRequest += Document_EncryptedFilePasswordRequest;
    }

    private static void Document_EncryptedFilePasswordRequest(object sender, EncryptedFilePasswordRequestEventArgs e) {
        if(e.DocumentName.EndsWith("AutoFilter.xlsx")) {
            e.Password = "123";
            e.Handled = true;
        }
    }
See Also