Luigi Arena
Luigi Arena

Reputation: 1

Blazor Web Assembly get value from page

I'have one page (Page1) that have 1 button and 1 textbox, when click on button I want to open a page that have a questionary, finish the questionary, the page is closed and I wanti to visualize in the text box (Page1) the result of the questionary. Thank.

Upvotes: 0

Views: 358

Answers (1)

Dylan El Bar
Dylan El Bar

Reputation: 943

If I understand your question:

  • You have a parent (button and text box)
  • You have a child, with an input, use write something here and then click on a button.
  • On click in the button, the text box of the parent is fed with the user input.

It seems that an Event will be your salvation. General idea could be

<h1>Parent</h1>
<p>@(ValueFromUser)</p>
<Child OnClose="OnCloseHandler"/>

@code {
    private string ValueFromUser { get; set; } = default!;

    private void OnCloseHandler(string value)
    {
        this.ValueFromUser = value;
    }
}

And

<h1>Child</h1>
<input @bind="UserInput" />
<button @onclick="OnClick">Click here</button>

@code {
    [Parameter] public EventCallback<string> OnClose { get; set; }
    private string UserInput { get; set; } = default!;

    private async Task OnClick()
    {
        await OnClose.InvokeAsync(UserInput);
    }
}

Adding some CSS rules for the "page" to be closed.

Upvotes: 1

Related Questions