aspnet-devexpress-dot-web-dot-aspxspreadsheet-dot-aspxspreadsheet-9089d45d.md
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
public static event InitializeDocumentEventHandler InitializeDocument
Public Shared Event InitializeDocument As InitializeDocumentEventHandler
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. |
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) {
//....
}
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.
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;
}
}
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