Version: 1.0 | 1.1 | 2.0 |

Button

in namespace DotVVM.Framework.Controls

Renders the HTML button which is able to trigger a postback.

Properties

Name Type Description Notes Default Value
ButtonTagName ButtonTagName Gets or sets whether the control should render the <input> or the <button> tag in the HTML.
attribute
inner element
static value
bindable
default
input
Enabled Boolean Gets or sets a value indicating whether the button is enabled and can be clicked on.
attribute
inner element
static value
bindable
default
False
IsSubmitButton Boolean Gets or sets whether the control should render a submit button or a normal button (type="submit" or type="button"). The submit button has some special features in the browsers, e.g. handles the Return key in HTML forms etc.
attribute
inner element
static value
bindable
default
False
Text String Gets or sets the text on the button.
attribute
inner element
static value
bindable
default

Events

Name Type Description
Click Command Gets or sets the command that will be triggered when the button is clicked.

Usage & Scenarios

This control renders HTML button which is able to trigger a postback.

HTML Rendered by the Control

Depending on the ButtonTagName property, the control renders one of these HTML elements:

<input type="submit" onclick="dotvvm.postBack(...);" value="Text" />
<button type="submit" onclick="dotvvm.postBack(...);">
	Text
</button>

Sample 1: Basic Usage

The Button control has a Text property that holds the text displayed in the button.

In the Click event you can specify which method will be called in the viewmodel.

<dot:Button Click="{command: Calculate()}" Text="Sample Button" />

<dot:Literal Text="{value: CurrentValue}"></dot:Literal>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Button.sample1
{
    public class ViewModel : DotvvmViewModelBase
    {
        public int CurrentValue { get; set; } = 0;

        public void Calculate()
        {
            CurrentValue++;
        }

    }
}

Sample 2: Disable or Hide the Button

To disable or enable the button, use the Enabled property. It supports the data-binding.

It's also possible to hide the button completely by using the Visible property.

<dot:Button Click="{command: SetVisible()}" Text="Button" Enabled="{value: Enabled}"/>

<dot:Button Click="{command: SetEnabled()}" Text="Invisible Button" Visible="{value: Visible}"/>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Button.sample2
{
    public class ViewModel : DotvvmViewModelBase
    {

        public bool Enabled { get; set; } = true;

        public bool Visible { get; set; } = false;

        public void SetEnabled()
        {
            Enabled = true;
            Visible = false;
        }

        public void SetVisible()
        {
            Visible = true;
            Enabled = false;
        }

    }

}

Sample 3: Button Types

The Button control has the ButtonTagName property which specifies the tag rendered by the button.
Pass either input (<input ... />) or button (<button ... />) values.

You can also choose whether it is a submit button or a normal button using the IsSubmitButton property.

<dot:Button Click="{command: Calculate()}" Text="Button with Input tag" ButtonTagName="input"/>

<dot:Button Click="{command: ShowMessage()}" Text="Submit Button" IsSubmitButton="true"/>

<dot:Literal Text="{value: Message}"/>

<dot:Literal Text="Current value: "></dot:Literal><dot:Literal Text="{value: CurrentValue}"/>

<dot:Literal Text="Submitted value: "></dot:Literal><dot:Literal Text="{value: SubmittedValue}"/>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Button.sample3
{
    public class ViewModel : DotvvmViewModelBase
    {
        public int SubmittedValue { get; set; } = 0;
        public int CurrentValue { get; set; } = 0;

        public string Message { get; set; } = "";

        public void Calculate()
        {
            CurrentValue++;
            Message = "";
        }

        public void ShowMessage()
        {
            Message = "Value submitted.";
            SubmittedValue = CurrentValue;
        }

    }
}

Sample 4: Image in the Button

The <button> tag supports more than just a text in the button. Instead of using the Text property, you can put the content inside the button.

<dot:Button ButtonTagName="button" Click="{command: Click()}">
    <img src="~/images/delete.png" alt="Delete" />
    Delete
</dot:Button>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Button.sample3
{
    public class ViewModel : DotvvmViewModelBase
    {
        
        public void Click()
        {
        }
        
    }
}

Sample 5: Async commands

All DotVVM controls support async methods in command bindings.

You should always use async Task instead of async void - it would probably end with an exception.

<dot:Button Click="{command: Calculate()}" Text="Sample Button" />

<dot:Literal Text="{value: CurrentValue}"></dot:Literal>
using DotVVM.Framework.ViewModel;
using System.Threading.Tasks;

namespace DotvvmWeb.Views.Docs.Controls.builtin.Button.sample5
{
    public class ViewModel : DotvvmViewModelBase
    {
        public int CurrentValue { get; set; } = 0;

        public async Task Calculate()
        {
            CurrentValue++;
        }

    }
}

Suggestions


Help & Contribute

The documentation is maintained on GitHub.
We'll be happy if you edit the page and create a pull request.

Send message