Content View - for Developers

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned

Overview

A Content View is an ASP.Net user control (in the form of an .ascx file in the Content Repository) that defines the layout of the HTML block that represents a Content. Content Views are built up of Field Controls - displaying values of Content Fields - embedded into custom HTML layout.

Details

Displaying a content view from code

To display a content with its appropriate contentview you can use the following line:

var contentView = ContentView.Create(content, this.Page, ViewMode.Browse);

You can chose from one of the following available viewmodes:

  • ViewMode.Browse
  • ViewMode.InlineEdit
  • ViewMode.InlineNew

The above code will use the automatic resolution logic to load the appropriate content view for the given content. You can also manually select the content view by giving the fourth parameter:

var contentView = ContentView.Create(content, this.Page, ViewMode.Browse, "$skin/contentviews/User/Browse.ascx");

You can read more about creating the ContentView object from code here:

A content view is an ASP.NET control and thus can be used in another user control or the control collection of a portlet, for example:

protected override void OnInit(EventArgs e)
{
    // display mycontent with its appropriate browse view
    var content = Content.Load("/Root/Sites/Default_Site/mycontent");
    var contentView = ContentView.Create(content, this.Page, ViewMode.Browse);
    this.Controls.Add(contentView);
 
    base.OnInit(e);
}

Content view events

There are some events you can use from code to customize UI processes in your Sense/Net ECMS.

CommandButtonsAction

In edit mode generic content views we usually work with the Command Buttons control, that handles the versioned saving of the displayed content. To intercept command buttons events, you can subscribe to a content view's CommandButtonsAction event. In the following example we define a simple user control that changes the display name of the saved content and saves the content manually:

    public class MyControl : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.Controls.Add(new LiteralControl { Text = "hello world" });
 
            var contentview = this.Parent as ContentView;
            contentview.CommandButtonsAction += new EventHandler<CommandButtonsEventArgs>(contentview_CommandButtonsAction);
 
            base.OnInit(e);
        }
 
        void contentview_CommandButtonsAction(object sender, CommandButtonsEventArgs e)
        {
            switch (e.ButtonType)
            {
                case CommandButtonType.CheckoutSaveCheckin:
                case CommandButtonType.SaveCheckin:
                    e.Cancel = true;
                    e.ContentView.Content.DisplayName = "custom saved content";
                    e.ContentView.Content.Save();
                    break;
            }
        }
    }

Place the control in any content view, and the above defined logic will be running whenever the user clicks the Done editing button:

<mycontrol:MyControl ID="test" runat="server" />

UserAction

The other useful action is the UserAction event. This can be triggered by placing a button in a SingleContentView content view (it won't work with other classes not inheriting from SingleContentView), whose OnClick handler calls the built-in Click function:

<mycontrol:MyControl ID="test" runat="server" />
<asp:Button ID="customButton" runat="server" OnClick="Click" CommandName="MyCustomCommand" Text="Click this!" />

The UserAction event will be triggered with the button's CommandName as the ActionName parameter in the EventArgs:

    public class MyControl : UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            var contentview = this.Parent as ContentView;
            contentview.UserAction += new EventHandler<UserActionEventArgs>(contentview_UserAction);
 
            this.Controls.Add(new LiteralControl { Text = "hello world" });
 
            base.OnInit(e);
        }
 
        void contentview_UserAction(object sender, UserActionEventArgs e)
        {
            if (e.ActionName == "MyCustomCommand")
                this.Controls.Add(new LiteralControl { Text = "clicked." });
        }
    }


Example/Tutorials

There are no examples for this article.

Related links

References

There are no related external articles for this article.