Creating a Single Add / Edit / Copy Record Page

Learn how to create a single Add / Edit / Copy Record page in Iron Speed Designer.
- Jing Ding, Senior Systems Consultant of The Ohio State University Medical Center

October 30, 2008
Iron Speed Designer V5.X

Introduction

Iron Speed Designer generates two separate pages to create new or edit existing records. These are the AddRecordPage, which can generate new records from scratch or copy them from existing records, and the EditRecordPage. The two pages look identical, and the differences between their code-behind and control files are minimal. Customization of page layout and business logic must be entered on both pages. To avoid excess overhead code, I recommend you merge the two pages rather than duplicate them.

Solution

Compare an AddRecordPage to its corresponding EditRecordPage using a DIFF application and you'll find the Controls.cs (vb) files are almost identical. Use one of these pages (for example, EditRecordPage) as a template and merge the differences from the other page using the following pattern:

If(page in Add/Copy mode){
    Copy lines from AddRecordPage
}else{
    Use lines from EditRecordPage
}

If your application has different business logic for Add / Copy / Edit pages, you must merge the business logic with the above pattern, too. The section below provides an example of merged ACEPage using EditRecordPage as the template. A bonus of using EditRecordPage as the template is that the copy mode is deep, i.e. not only the main record is copied, but also its detail records.

Implementation

Step 1: Define Page Mode
The first step to merge Add and Edit pages is to define the page mode. There are many ways to do this. This example will use two URL parameters, RecordID and Copy, to define the page mode.

Mode RecordID Copy
Add Null Null
Copy Not null Not null
Edit Not null Null

Step 2: Access Page Mode
Once the page mode is defined, it is straight forward to access the mode in the page class or the Control classes. For example, the following code snippet accesses the page mode within a control class.

C#:

if (Page.Request.QueryString[“RecordID”] == null) {
    // Add mode.
} else if (Page.Request.QueryString[“Copy”] == null) {
    // Edit mode.
} else {
    // Copy mode.
}

Visual Basic .NET:

If Page.Request.QueryString("RecordID") = Nothing Then
    ' Add mode.
ElseIf Page.Request.QueryString("Copy") = Nothing Then
    ' Edit mode.
Else
    ' Copy mode.
End If

However, if you have a lot of business logic to merge, this may clutter your code. A more elegant solution is to define the page mode as a virtual property in the BaseApplicationPage class. This makes it more easily accessible anywhere with any Iron Speed Designer-generated pages.

First, insert the following code into the BaseApplicationPage class (located in app_root\App_Code\Shared\BaseApplicationPage.cs or vb).

C#:

public enum Mode { Add, Copy, Edit, Other }
 
public virtual Mode AceMode {
    get { return Mode.Other; }
}

Visual Basic .NET:

Public Enum Mode
    Add
    Copy
    Edit
    Other
End Enum
 
Public Overridable ReadOnly Property AceMode() As Mode
    Get
        Return Mode.Other
    End Get
End Property

Then in the ACEPage class, override the property.

C#:

public override BaseApplicationPage.Mode AceMode {
  get {
    if (Request.QueryString["RecordID"] == null)
        return Mode.Add;
    else if (Request.QueryString["Copy"] == null)
        return Mode.Edit;
    else
        return Mode.Copy;
  }
}

Visual Basic .NET:

Public Overloads Overrides ReadOnly Property AceMode() As BaseApplicationPage.Mode
  Get
    If Request.QueryString("RecordID") = Nothing Then
      Return Mode.Add
    ElseIf Request.QueryString("Copy") = Nothing Then
      Return Mode.Edit
    Else
      Return Mode.Copy
    End If
  End Get
End Property

Step 3: Merge Main Record Control
In the main record class, override the CreateWhereClause() and GetUIData() methods, as well as the GetRecord() method.

C#:

public override WhereClause CreateWhereClause() {
  if (Page.AceMode == BaseApplicationPage.Mode.Add)
    return null;
  return base.CreateWhereClause();
}
 
public override void GetUIData() {
  if (Page.AceMode == BaseApplicationPage.Mode.Copy)
    DataSource = new MyRecord();
  base.GetUIData();
}
 
public MyRecord GetRecord() {
  if (this.DataSource != null)
    return this.DataSource;
 
  if (this.RecordUniqueId != null)
    return MyTable.GetRecord(this.RecordUniqueId, true);
  else
        return new MyRecord();
}

Visual Basic .NET:

Public Overloads Overrides Function CreateWhereClause() As WhereClause
  If Page.AceMode = BaseApplicationPage.Mode.Add Then
    Return Nothing
  End If
  Return MyBase.CreateWhereClause()
End Function
 
Public Overloads Overrides Sub GetUIData()
  If Page.AceMode = BaseApplicationPage.Mode.Copy Then
    DataSource = New MyRecord()
  End If
  MyBase.GetUIData()
End Sub
 
Public Function GetRecord() As MyRecord
  If Me.DataSource <> Nothing Then
    Return Me.DataSource
  End If
 
  If Me.RecordUniqueId <> Nothing Then
    Return MyTable.GetRecord(Me.RecordUniqueId, True)
  Else
    Return New MyRecord()
  End If
End Function

Step 4: Merge Detail Table Controls
If the page contains any detail tables, override their TableControls’ CreateWhereClause() methods.

C#:

public override WhereClause CreateWhereClause() {
  if (Page.AceMode == BaseApplicationPage.Mode.Add) {
    WhereClause wc = new WhereClause();
    wc.RunQuery = false;
    return wc;
  } else
    return base.CreateWhereClause();
}

Visual Basic .NET:

Public Overloads Overrides Function CreateWhereClause() As WhereClause
  If Page.AceMode = BaseApplicationPage.Mode.Add Then
    Dim wc As New WhereClause()
    wc.RunQuery = False
    Return wc
  Else
    Return MyBase.CreateWhereClause()
  End If
End Function

Within each detail table’s TableControlRow class, override the LoadData() method.

C#:

public override void LoadData() {
  if (Page.AceMode == BaseApplicationPage.Mode.Copy)
DataSource = new MyRecord();
    else
    base.LoadData();
}

Visual Basic .NET:

Public Overloads Overrides Sub LoadData()
  If Page.AceMode = BaseApplicationPage.Mode.Copy Then
    DataSource = New MyRecord()
  Else
    MyBase.LoadData()
  End If
End Sub

Conclusion

Merge two pages into an ACEPage when you have a large amount of code customization in your application. This is especially helpful when you would like to duplicate page layout and business logic customizations from Add / Copy pages to an Edit page.

About the Author

Jing Ding has a PhD in Computer Engineering, Bioinformatics and Computational Biology, and an M.S. in Toxicology from Iowa State University. He received his B.S. in biophysics from Fundan University in Shanghai, China. He is a self-taught programmer who "played" with assembly, C and C++ in the 1990s. He took a break from programming from 1997 to 2000. When he picked it up again in 2001, he worked with Java. Jing began working with C# and .NET in 2006.


  Privacy Statement