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