SetVar Element (View)
Published: May 2010
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.
The SetVar element has both a spanning and a non-spanning 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 often a child to a Method element used in batch processing of requests.
The SetVar element is frequently used to change the display mode by setting DisplayMode to one of the following values.
Used when rendering the ViewBody section of a view.
Used when rendering an edit item form.
Used when rendering the ViewHeader section of a view.
Used when rendering a new item form.
Used when editing a display form with Microsoft FrontPage.
Used when editing a new item form with FrontPage.
Used when editing an edit item form with FrontPage.
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.
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.