Declaring Server-Side Event Handlers
- 2 minutes to read
Certain DevExpress ASP.NET MVC extensions expose event-driven functionality, allowing you to respond to certain stages of component creation and rendering. By handling these events, which are based on the delegate model, you can control and customize the default rendering of an extension.
The code example below demonstrates how to define the server event handler. In this example, the Menu’s MenuSettingsBase.ItemDataBound event is processed.
View:
@Html.DevExpress().Menu(
settings => {
settings.Name = "myMenu";
settings.Orientation = System.Web.UI.WebControls.Orientation.Vertical;
settings.EncodeHtml = false;
settings.ItemDataBound = (sender, e) => {
var node = e.Item.DataItem as SiteMapNode;
if (node != null) {
if (node["title"] == "USA") {
e.Item.Text = string.Format("<b>{0}</b>", e.Item.Text);
}
}
};
}).BindToSiteMap("~/App_Data/WorldCup2010.sitemap", false).GetHtml()
Accessing the Wrapped ASP.NET Web Forms Control
The DevExpress ASP.NET MVC extensions are the wrappers of DevExpress ASP.NET Web Forms controls. These wrappers are designed to follow the MVC pattern and seamlessly integrate into ASP.NET MVC projects.
When a DevExpress ASP.NET MVC extension is configured and bound to data (if available), the extension’s instance is represented by the MVCx[ExtensionName] class. This class is a descendant of the corresponding DevExpress ASP.NET Web Forms control, for example, the MVCxGridView class is a descendant of ASPxGridView.
You can access the wrapped control by handling the SettingsBase.PreRender or the SettingsBase.Init server event. The event sender is the corresponding MVCx[ExtensionName] object. Using the MVCx[ExtensionName] objects can be useful for modifying extension behavior if the behavior depends on data items displayed within the extension. If extension behavior does not depend on data, it is recommended that you modify the behavior using the [ExtensionName]Settings object.
Example
The code sample below demonstrates how to access the MVCxGridView object and expand the first detail row when the grid is initially displayed in a page.
@Html.DevExpress().GridView(
settings =>
{
settings.Name = "grid";
// ...
// Expand the first detail row.
settings.PreRender = (sender, e) => {
((MVCxGridView)sender).DetailRows.ExpandRow(0);
};
}).Bind(Model).GetHtml()