Back to Devexpress

ASPxSpreadsheet.InitializeDocument Event

aspnet-devexpress-dot-web-dot-aspxspreadsheet-dot-aspxspreadsheet-9089d45d.md

latest6.3 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Web.Office

Declaration

csharp
public static event InitializeDocumentEventHandler InitializeDocument
vb
Public Shared Event InitializeDocument As InitializeDocumentEventHandler

Event Data

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

PropertyDescription
DocumentProvides 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:

csharp
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) {  
    //....  
}
vb
Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)  
    AddHandler ASPxSpreadsheet.InitializeDocument, AddressOf ASPxSpreadsheet_InitializeDocument  
End Sub  
Protected Shared Sub ASPxSpreadsheet_InitializeDocument(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxSpreadsheet.SpreadsheetInitializeDocumentEventArgs)  
    AddHandler e.Document.DocumentLoaded, AddressOf Document_DocumentLoaded  
End Sub  
Private Shared Sub Document_DocumentLoaded(ByVal sender As Object, ByVal e As EventArgs)  
    '....  
End Sub

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.

aspx
<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>
csharp
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;
        }
    }
vb
Imports DevExpress.Spreadsheet
Imports DevExpress.Web.ASPxSpreadsheet
Imports DevExpress.Web.Office

' ...
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then ReopenDocument()
    End Sub

    Protected Sub ReopenDocument()
        AddHandler ASPxSpreadsheet.InitializeDocument, AddressOf ASPxSpreadsheet_InitializeDocument
        Dim filePath As String = Server.MapPath("~/App_Data/WorkDirectory/AutoFilter.xlsx")
        DocumentManager.CloseDocument(filePath)
        ASPxSpreadsheet1.Open(filePath)
    End Sub

    Protected Shared Sub ASPxSpreadsheet_InitializeDocument(ByVal sender As Object, ByVal e As SpreadsheetInitializeDocumentEventArgs)
        AddHandler e.Document.EncryptedFilePasswordRequest, AddressOf Document_EncryptedFilePasswordRequest
    End Sub

    Private Shared Sub Document_EncryptedFilePasswordRequest(ByVal sender As Object, ByVal e As EncryptedFilePasswordRequestEventArgs)
        If e.DocumentName.EndsWith("AutoFilter.xlsx") Then
            e.Password = "123"
            e.Handled = True
        End If
    End Sub

See Also

ASPxSpreadsheet Class

ASPxSpreadsheet Members

DevExpress.Web.ASPxSpreadsheet Namespace