Server Scripts and Events
Server scripts allow you to control form behaviour during key points in the form lifecycle.
They can be used to initialise data, respond to form submissions, define complex data sources, and apply custom logic where required.
Form Lifecycle Methods
Activate Web Forms support several server-side lifecycle methods. Each method runs at a different stage of the form process.
Form_Init
Form_Init is called before any controls are created on the form.
Use this method to prepare data before the form is rendered.
Common uses include:
- Setting job arguments
- Loading data sources
- Preparing values used by controls
- Applying conditional form behaviour before controls are generated
Example:
public void Form_Init(ActivateHttpRequest request) { Job.Arguments.SetValue("resource", "=//Resources/Configuration"); }
Form_Load
Form_Load is called after the controls have been loaded.
At this point, GetControl() is available.
However, most form logic should be handled in Form_Init, because it is usually better to prepare the data before controls are created rather than modify controls after they have loaded.
Use Form_Load only when you specifically need to work with controls after they exist.
Example:
public void Form_Load(object sender, EventArgs e) {
ProvisioningTrace.Forms("Form Load");
}
Form_Submit
Form_Submit is called when the form is submitted.
Unlike earlier form behaviour, controls are not recreated during postback, so server controls are not available inside this method.
Use Form_Submit to respond to user actions and submission events.
Common uses include:
- Handling button actions
- Performing custom validation
- Displaying page, control, or popup messages
- Updating job arguments
- Returning to a previous page
- Redirecting to the next page
Example:
public void Form_Submit(ActivateHttpRequest request)
{
if (request.IsEventSource("ValidateName"))
{
string name = Job.Arguments.GetString("data/name");
request.Page.SetControlError("txtName1", "Can't find user [" + name + "]");
}
if (request.IsEventSource("SendEmail"))
{
request.Page.SetPopupMessage("Email Sent");
}
}Using Event Sources
Event sources identify which form action triggered the submit event.
This allows a single Form_Submit method to handle multiple actions cleanly.
Example:
if (request.IsEventSource("GeneratePageError"))
{
request.Page.SetPageError("Please select at least one user to request information from.");
}Use event source checks to separate submit logic by button, action, or form event.
Server Script Availability
The server script is available as a global variable within the form.
This can be used in the form definition for more advanced scenarios, such as complex data sources or conditional logic.
Recommended Approach
Use each lifecycle method for its intended purpose:
- Use Form_Init to prepare data before controls are created
- Use Form_Load only when control-level access is required
- Use Form_Submit to handle submitted actions
- Use event sources to keep submit logic clear and maintainable
Best Practice
Prefer setting data in Form_Init and allowing controls to load from that data naturally.
Avoid changing control properties in Form_Load unless there is a specific reason to access the control directly.
Keep Form_Submit focused on handling user actions rather than preparing initial form data.