Version

Adding a Behavior at Runtime

Enabling a behavior at runtime is simple; you only need to call the generic CreateBehavior method. This method requires a specific behavior type. It creates the behavior and adds it to the Behaviors collection. Every behavior has a class that represents the type of behavior (e.g., Selection ).

The following table lists the behaviors and their corresponding classes.

Behavior Class Name

Activation

Selection

Row Selectors

Grid Editing

Cell Editing

Row Adding

Row Deleting

Row Editing

Edit Template

Filtering

Sorting

Paging

Column Resizing

WebDataGrid™ provides read-only properties of the same name as the classes previously listed so that you can conveniently access a behavior once you have added it to the behaviors collection.

The following code shows how to add the selection behavior to WebDataGrid using the CreateBehavior method. It also shows the equivalent code if you decide to create a behavior and add it manually.

Note
Note:

As with adding controls dynamically, if you create a behavior manually, make sure you add the behavior to the Behaviors collection before setting its properties in order to keep those settings in ViewState.

In Visual Basic:

Dim gridSelection As Selection = Me.WebDataGrid1.Behaviors.CreateBehavior(Of Selection)()
gridSelection.CellClickAction = CellClickAction.Row
gridSelection.RowSelectType = SelectType.Multiple
' THE EQUIVALENT CODE IS SHOWN BELOW
' To get reference to a behavior, use its property of the same name
Dim selectionBehavior As Selection = Me.WebDataGrid1.Behaviors.Selection
' Check if behavior does not exist
If selectionBehavior Is Nothing Then
        ' Create the behavior
        Dim selection As New Selection()
        ' Add to behaviors collection first before setting its properties
        Me.WebDataGrid1.Behaviors.Add(selection)
        ' Set its options
        selection.CellClickAction = CellClickAction.Row
        selection.RowSelectType = SelectType.Multiple
End If

In C#:

Selection gridSelection = this.WebDataGrid1.Behaviors.CreateBehavior<Selection>();
gridSelection.CellClickAction = CellClickAction.Row;
gridSelection.RowSelectType = SelectType.Multiple;
// THE EQUIVALENT CODE IS SHOWN BELOW
// To get reference to a behavior, use its property of the same name
Selection selectionBehavior = this.WebDataGrid1.Behaviors.Selection;
// Check if behavior does not exist
if (selectionBehavior == null)
{
        // Create the behavior
        Selection selection = new Selection();
        // Add to behaviors collection
        this.WebDataGrid1.Behaviors.Add(selection);
        // Set its options
        selection.CellClickAction = CellClickAction.Row;
        selection.RowSelectType = SelectType.Multiple;
}