This example illustrates how to switch data sources used for Mail Merge in ASPxRichEdit at runtime.
To update ASPxRichEdit’s data source dynamically, wrap it in the ASPxCallbackPanel control and use its callbacks to refresh data.
Note that ASPxRichEdit should be bound to actual data on every round-trip to the server, so it is insufficient to set the data source in the server-side Callback event handler only. It’s necessary to restore the actual data source and bind ASPxRichEdit to it on each request in the Page_Init event handler.
In addition, it is necessary to clear the current document in ASPxRichEdit each time the data source is changed. For this purpose, you can use the server-side New() method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class MergeDataModels
{
public static List<TestMergeModel1> GetTestData1() {
var ds = new List<TestMergeModel1>();
for (int i = 0; i < 10; i++) {
ds.Add(new TestMergeModel1 {
FirstName1 = "First Name_1_" + i,
LastName1 = "LastName_1_" + i,
Address1 = "Address_1_" + i
});
}
return ds;
}
public static List<TestMergeModel2> GetTestData2() {
var ds = new List<TestMergeModel2>();
for (int i = 0; i < 10; i++) {
ds.Add(new TestMergeModel2 {
FirstName2 = "First Name_2_" + i,
LastName2 = "LastName_2_" + i,
Address2 = "Address_2_" + i
});
}
return ds;
}
}
public class TestMergeModel1
{
public string FirstName1 { get; set; }
public string LastName1 { get; set; }
public string Address1 { get; set; }
}
public class TestMergeModel2
{
public string FirstName2 { get; set; }
public string LastName2 { get; set; }
public string Address2 { get; set; }
}
using DevExpress.Web;
using DevExpress.Web.Office;
public partial class _Default : System.Web.UI.Page {
protected void Page_Init(object sender, EventArgs e) {
if (Session["dataMerge"] != null) {
ASPxRichEdit1.DataSource = Session["dataMerge"];
ASPxRichEdit1.DataBind();
}
}
protected void ASPxCallbackPanel1_Callback(object sender, CallbackEventArgsBase e) {
ASPxRichEdit1.New();
if (Convert.ToInt32(ASPxComboBox1.Value) == 1)
Session["dataMerge"] = MergeDataModels.GetTestData1();
else
Session["dataMerge"] = MergeDataModels.GetTestData2();
ASPxRichEdit1.DataSource = Session["dataMerge"];
ASPxRichEdit1.DataBind();
}
}
<script>
function onSelectedIndexChanged(s, e) {
cp.PerformCallback();
}
</script>
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" ClientInstanceName="cb" ValueType="System.Int32" Width="210">
<Items>
<dx:ListEditItem Value="1" Text="Data source for Mail Merge #1" />
<dx:ListEditItem Value="2" Text="Data source for Mail Merge #2" />
</Items>
<ClientSideEvents SelectedIndexChanged="onSelectedIndexChanged" />
</dx:ASPxComboBox>
<br />
To check that the data source is applied, try to insert mail merge fields and view merged data
<dx:ASPxCallbackPanel ID="ASPxCallbackPanel1" runat="server" ClientInstanceName="cp" OnCallback="ASPxCallbackPanel1_Callback">
<PanelCollection>
<dx:PanelContent runat="server" SupportsDisabledAttribute="True">
<dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server" WorkDirectory="~\App_Data\WorkDirectory">
<Settings>
<Behavior CreateNew="Hidden" Save="Hidden" Open="Hidden" SaveAs="Hidden" />
</Settings>
</dx:ASPxRichEdit>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
Imports DevExpress.Web
Imports DevExpress.Web.Office
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
If Session("dataMerge") IsNot Nothing Then
ASPxRichEdit1.DataSource = Session("dataMerge")
ASPxRichEdit1.DataBind()
End If
End Sub
Protected Sub ASPxCallbackPanel1_Callback(ByVal sender As Object, ByVal e As CallbackEventArgsBase)
ASPxRichEdit1.[New]()
If Convert.ToInt32(ASPxComboBox1.Value) = 1 Then
Session("dataMerge") = MergeDataModels.GetTestData1()
Else
Session("dataMerge") = MergeDataModels.GetTestData2()
End If
ASPxRichEdit1.DataSource = Session("dataMerge")
ASPxRichEdit1.DataBind()
End Sub
End Class
Public NotInheritable Class MergeDataModels
Private Sub New()
End Sub
Public Shared Function GetTestData1() As List(Of TestMergeModel1)
Dim ds = New List(Of TestMergeModel1)()
For i As Integer = 0 To 9
ds.Add(New TestMergeModel1 With {.FirstName1 = "First Name_1_" & i, .LastName1 = "LastName_1_" & i, .Address1 = "Address_1_" & i})
Next i
Return ds
End Function
Public Shared Function GetTestData2() As List(Of TestMergeModel2)
Dim ds = New List(Of TestMergeModel2)()
For i As Integer = 0 To 9
ds.Add(New TestMergeModel2 With {.FirstName2 = "First Name_2_" & i, .LastName2 = "LastName_2_" & i, .Address2 = "Address_2_" & i})
Next i
Return ds
End Function
End Class
Public Class TestMergeModel1
Private privateFirstName1 As String
Public Property FirstName1() As String
Get
Return privateFirstName1
End Get
Set(ByVal value As String)
privateFirstName1 = value
End Set
End Property
Private privateLastName1 As String
Public Property LastName1() As String
Get
Return privateLastName1
End Get
Set(ByVal value As String)
privateLastName1 = value
End Set
End Property
Private privateAddress1 As String
Public Property Address1() As String
Get
Return privateAddress1
End Get
Set(ByVal value As String)
privateAddress1 = value
End Set
End Property
End Class
Public Class TestMergeModel2
Private privateFirstName2 As String
Public Property FirstName2() As String
Get
Return privateFirstName2
End Get
Set(ByVal value As String)
privateFirstName2 = value
End Set
End Property
Private privateLastName2 As String
Public Property LastName2() As String
Get
Return privateLastName2
End Get
Set(ByVal value As String)
privateLastName2 = value
End Set
End Property
Private privateAddress2 As String
Public Property Address2() As String
Get
Return privateAddress2
End Get
Set(ByVal value As String)
privateAddress2 = value
End Set
End Property
End Class
<script>
function onSelectedIndexChanged(s, e) {
cp.PerformCallback();
}
</script>
Choose the required Mail Merge data source:
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" ClientInstanceName="cb" ValueType="System.Int32" Width="210">
<Items>
<dx:ListEditItem Value="1" Text="Data source for Mail Merge #1" />
<dx:ListEditItem Value="2" Text="Data source for Mail Merge #2" />
</Items>
<ClientSideEvents SelectedIndexChanged="onSelectedIndexChanged" />
</dx:ASPxComboBox>
<br />
To check that the data source is applied, try to insert mail merge fields and view merged data
<dx:ASPxCallbackPanel ID="ASPxCallbackPanel1" runat="server" ClientInstanceName="cp" OnCallback="ASPxCallbackPanel1_Callback">
<PanelCollection>
<dx:PanelContent runat="server" SupportsDisabledAttribute="True">
<dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server" WorkDirectory="~\App_Data\WorkDirectory">
<Settings>
<Behavior CreateNew="Hidden" Save="Hidden" Open="Hidden" SaveAs="Hidden" />
</Settings>
</dx:ASPxRichEdit>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>