---
title: Server Scripts and Events
slug: server-scripts-and-events
docTags: 
createdAt: 2025-06-11T20:26:19.223Z
---

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&#x20;
- Loading data sources&#x20;
- Preparing values used by controls&#x20;
- Applying conditional form behaviour before controls are generated&#x20;

Example:

:::BlockQuote
public void Form\_Init(ActivateHttpRequest request)
\{
&#x20;    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:

:::BlockQuote
public void Form\_Load(object sender, EventArgs e)
\{

&#x20;   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&#x20;
- Performing custom validation&#x20;
- Displaying page, control, or popup messages&#x20;
- Updating job arguments&#x20;
- Returning to a previous page&#x20;
- Redirecting to the next page&#x20;

Example:

```csharp
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:

```csharp
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&#x20;
- Use Form\_Load only when control-level access is required&#x20;
- Use Form\_Submit to handle submitted actions&#x20;
- Use event sources to keep submit logic clear and maintainable&#x20;

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