Skip to main content

How to: Change the Mail Merge data sources at runtime

  • 5 minutes to read

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; }
}