Home

Controls

As seen in the section on forms, Vici MVC will render HTML controls bound to fields in a class. This is done by adding an attribute to a field, which tells the framework what control to render and how it should look/behave.

You can also create controls explicitly (in code) and render them in the view. For example, to render a text box created from code:

TextControl control = new TextControl("Name");

control.HandlePostback(); // if there is a postback, the 
                          // contents of the textbox is
                          // stored in the control's
                          // Value property

if (IsPost())
{
    string postedName = control.Value;

    // Do something
}
else
{
    control.Value = "John Doe"; // initial contents
}

ViewData["NameBox"] = control; // pass it to the view

The view then looks like:

Name: [[NameBox]]

Built-in controls

All built-in controls have the following properties in common:

PropertyTypeDescription
IdStringThe XHTML id to render the control with. This should be unique across the rendered page. If not set, the framework will generate a unique id for you.
NameStringThe name of the control, which is required and should only be set by the constructor
ErrorBooleanIf set to true, the control will be renderd with the CSS class defined by the CssClassError property. If false, the CSS class defined by CssClass will be used
CssClassStringThe CSS class name to use when rendering the control (if the Error property is false)
CssClassErrorStringThe CSS class name to use when rendering the control (if the Error property is true)
AutoPostBooleanSetting this to true will render the attribute onchange="this.form.submit()"
OnChangeStringDefines a line of javascript to execute when the control is changed on the client. The javascript will be prepended to the javascript generated by the AutoPost property
EnabledBooleanSetting this to false causes the control to be renderd with the disabled="disabled" attribute

TextBoxControl

Renders a text box (<input type="text"/>).

Specific properties:

PropertyTypeDescription
ValueStringThe text in the textbox
MaxLengthintThe maximum length of the text in the text box (maxlength attribute)
AutoCompleteboolWhen set to false, the attribute autocomplete="off" will be rendered
OnKeyPressstringAdds the specified javascript in the onkeypress attribute
OnKeyDownstringAdds the specified javascript in the onkeydown attribute
OnKeyUpstringAdds the specified javascript in the onkeyup attribute
DefaultCssClassstring(static) Sets the default value for the CssClass property of all TextBoxControl instances
DefaultCssClassErrorstring(static) Sets the default value for the CssClassError property of all TextBoxControl instances

DropdownControl

Renders a dropdown box (<select>).

Specific properties:

PropertyTypeDescription
ValueobjectThe currently selected value in the list
DataSourceobjectA list of objects/values to bind to the control (explained below)
ItemsList<DropdownControl.Item>Contains all items in the list box (explained below)
KeyMemberstringThe name of the field or property containing the unique key of the items in the dropdown control. When this property is not set, the object itself will be used as the key (useful when the DataSource is an array)
ValueMemberstringThe name of the field or property containing the value (visible to the user) of the items in the dropdown control. When this property is not set, the object itself (converted to a string) will be used to display items in the dropdown list (useful when the DataSource is an array)
ShowBlankBooleanIf set to true, a blank item will be added to the list (as the first item)
BlankKeyobjectThe value to store in the Value property when the blank item is selected
BlankValueobjectWhat to show in the dropdown control as the blank value
ValueFormatStringstringWhen set, a standard .NET format string will be used to show the items in the list. Use {0} for the key and {1} for the value
DefaultCssClassstring(static) Sets the default value for the CssClass property of all DropdownControl instances
DefaultCssClassErrorstring(static) Sets the default value for the CssClassError property of all DropdownControl instances

Filling the dropdown list

There are 2 properties available for specifying the items to display in the dropdown control: DataSource and Items. Either or both can be used. The behavior is as follows:

  • When setting the DataSource, the Items list is cleared. This means that you should never set the DataSource property after manipulating the Items list.
  • When retrieving the Items list, the list is populated with data from the data source
  • It is possible to add items or remove items from the list

The Items list is of type List<Item>. The Item class has only 2 properties: Key and Value, both of type object. Make sure that you store the right types in the item list.

When handling a postback, it is not necessary to populate the control again. Just retrieve the Value property.

Setting the Value property can be done anytime, even before items are added to the list. The matching of the value with an item from the list is done at render time, not when setting the Value property.

HiddenControl

Renders a hidden control (<input type="hidden" />)

There is just one property:

PropertyTypeDescription
ValuestringThe value attribute of the control

CheckboxControl

Renders a checkbox control (<input type="checkbox"/>)

Properties:

PropertyTypeDescription
CheckedBooleanTrue means the checkbox is checked
OnClickStringAdds the specified javascript in the onclick attribute

MemoControl

Renders a textarea control (<textarea>)

Properties:

PropertyTypeDescription
ValueStringThe text in the textbox
WidthInt32The width of the text box in characters (it's not recommended to be used, CSS styling is preferred)
HeightInt32The height of the text box in lines (it's not recommended to be used, CSS styling is preferred)
OnKeyPressstringAdds the specified javascript in the onkeypress attribute
OnKeyDownstringAdds the specified javascript in the onkeydown attribute
OnKeyUpstringAdds the specified javascript in the onkeyup attribute

RadioButtonControl

Renders a checkbox control (<input type="checkbox"/>)

Properties:

PropertyTypeDescription
CheckedBooleanTrue means the radio button is checked
GroupStringThe group name for this radio button. Only one radio button can be checked per group
OnClickStringAdds the specified javascript in the onclick attribute

Built-in form field attributes

For mapping fields to controls in a form, you have to add form field attributes to a field. This is discussed in the [[Projects:Mvc:UserGuide:FormBasics|forms] section.

The following built-in attributes are available:

[FormTextBox]

To be completed

[FormDropdown]

To be completed

[FormPassword]

To be completed

[FormEMail]

To be completed

[FormHidden]

To be completed

[FormCheckbox]

To be completed

[FormDate]

To be completed

[FormMemo]

To be completed

[FormRadioButton]

To be completed