Partager via


SetVar Element

The SetVar element allows variables to be set in the context of rendering the page—either locally to the current level of XML or globally to the page.

Syntax

<SetVar
  ID = "Text"
  Name = "Text"
  Scope = "Request"
  Value = "Text">
</SetVar>

Attributes

Name Description
ID Optional Text. Provides an ID for the variable.
Name Required Text. Specifies a name for the variable.
Scope If set to Request, the variable is global.
Value Optional Text. Can be used to assign a value to the variable when this is an empty element.

Remarks

The SetVar element has both a spanning and a nonspanning form, so that <SetVar Name="MyVar">Announcements</SetVar> would be the same as <SetVar Name="MyVar" Value="Announcements/>.

If Scope="Request" is specified in the SetVar element, a variable assignment takes effect anywhere in the current page. Otherwise, the assignment affects only children of the SetVar element.

The SetVar element is frequently used to change the display mode by setting DisplayMode to one of the following values.

DISPLAY — when rendering the ViewBody section of a view.
EDIT — when rendering an edit item form.
DISPLAYHEAD — when rendering the ViewHeader section of a view.
NEW — when rendering a new item form.
PREVIEWDISPLAY — when editing a display form with Microsoft FrontPage.
PREVIEWNEW — when editing a new item form with FrontPage.
PREVIEWEDIT — when editing an edit item form with FrontPage.

Example

The following example illustrates using the SetVar element to set global scope for a variable. The second line returns the value set in the first line.

<SetVar Name="GlobalVar" Scope="Request">Value</SetVar>
.
.
.
<GetVar Name="GlobalVar">

In the following example, the first GetVar element returns "Value_2", and the second GetVar element returns "Value_1", because the SetVar element that contains "Value_2" applies only to children of the "Sample" element. "Value_2" goes out of scope after the closing "Sample" tag.

<SetVar Name="myVar">Value_1</SetVar>
   <Sample>
      <SetVar Name="myVar">Value_2</SetVar>
      <GetVar Name="myVar"/>
   </Sample>
   <GetVar Name="myVar"/>

The following example evaluates whether a field is required and, if it is required, creates the red asterisk (*) that is displayed beside required fields in New or Edit forms and sets the HasRequired variable to TRUE.

<Switch>
   <Expr>
      <Property Select="Required"/>
   </Expr>
   <Case Value="TRUE">
      <HTML><![CDATA[<font color=red> *</font>]]></HTML>
      <SetVar Scope="Request" Name="HasRequired">TRUE</SetVar>
   </Case>
</Switch>