Dashboard > Spring.NET > ... > Spring.Web > Spring.Web Client-Side Scripting Support
  Spring.NET Log In View a printable version of the current page.  
  Spring.Web Client-Side Scripting Support
Added by Aleksandar Seovic, last edited by Mark Pollack on Nov 27, 2005  (view change)
Labels: 
(None)

Warning

This documentation has been moved to docbook format and now resides on the Spring.NET website. If you feel the need to edit this document please send and email to the Spring.NET developer list.

ASP.NET has a decent support for client-side scripting through Page.RegisterClientScruptBlock and Page.RegisterStartupScript methods.

However, neither of these two methods allows you to output registered script markup within head section of the page, which is in many cases exactly what you would like to do.

Registering Scripts within head HTML section

Spring.Web adds several methods to the base Spring.Web.UI.Page class: RegisterHeadScriptBlock and RegisterHeadScriptFile, each with a few overrides. You can call these methods from your custom pages and controls in order to register script blocks and script files that should be included in the head section of the final HTML file.

The only additional thing that is required to make this work is that you use <spring:Head> server-side control to define your head section instead of using standard HTML head element:

Setting up Head section
<%@ Page language="c#" Codebehind="StandardTemplate.aspx.cs" AutoEventWireup="false" Inherits="SpringAir.Web.StandardTemplate" %>
<%@ Register TagPrefix="spring" Namespace="Spring.Web.UI.Controls" Assembly="Spring.Web" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
  <spring:Head runat="server" id="Head1">
    <title>
      <spring:ContentPlaceHolder id="title" runat="server">
        <%= GetMessage("default.title") %>
      </spring:ContentPlaceHolder>
    </title>
    <LINK href="<%= CssRoot %>/default.css" type="text/css" rel="stylesheet">
    <spring:ContentPlaceHolder id="head" runat="server"></spring:ContentPlaceHolder>
  </spring:Head>
  <body>
  ...
  </body>
</html>

The example above shows you how you would typically setup head section within a master page template in order to be able to change title value and to add additional elements to the head section from the child pages using <spring:ContentPlaceholder> controls. However, only <spring:Head declaration is required in order for Spring.NET Register* scripts to work properly.

Adding CSS Definitions to the head Section

In a similar fashion, you can add references to CSS files, or even specific styles directly to the head HTML section using Page.RegisterStyle and Page.RegisterStyleFile methods. Latter one simply allows you to include reference to an external CSS file, while the former one allows you to define embedded style definitions by specifying style name and style definition as the parameters. Final list of style definitions registered this way will be rendered within single embedded style section of the final HTML document.

Spring.Web "Well-Known" Directories

In order to make manual inclusion of client-side scripts, CSS files and images easier, Spring.Web Page class exposes several properties that help you reference those artifacts using absolute path.

These properties are ScriptsRoot, CssRoot and ImagesRoot. They have default values of Scripts, CSS and Images, which will work just fine if you create and use these directories in your web application root. However, if you prefer to place them somewhere else, you can always override default values by injecting new values into your page definitions (you will typically inject these values only in the base page definition, as they are normally shared by all the pages in the application):

Overriding Values for "Well-Known" Directories
<object id="basePage" abstract="true">
    <description>
        Convenience base page definition for all the pages.
        
        Pages that reference this definition as their parent
        (see examples below) will automatically inherit following properties.
    </description>
    <property name="CssRoot" value="Web/CSS"/>
    <property name="ImagesRoot" value="Web/Images"/>
</object>

Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators