blazor.net-5

How can I get a reference to a blazer control in code


How can I get a reference to a blazer control in code. I would like to change the control in code.


Solution

  • You have to use @ref on component declaration.

    At next step, you have to define a variable with component type.

    Now, you can access the component with variable.

    <InputText @ref="PersonalNameTextbox" ...
    

    In @code or razor.cs, define a variable:

    InputText PersonalNameTextbox;
    

    Example of access the component in afterRender, you can access them:

    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
            PersonalNameTextbox.Value = "Untitled!";
        ...
    }