1

I am trying to get a child component to update a list on the parent. To do this I setup an EventCallback that takes a list and sends it to the parent. The issue is the event never fires and the HasDelegate variable on the callback is false.

Parent .razor.cs:
        public async Task UpdateSelectedCompanies(List<CompanyStub> companies)
        {
            _selectedCompanies = companies;

            await InvokeAsync(StateHasChanged);
        }

Parent .razor:
        <CompanyTable IncludeCheckbox="true" UpdateCompanies="@UpdateSelectedCompanies"></CompanyTable>

Child .razor.cs:
        [Parameter] public EventCallback<List<CompanyStub>> UpdateCompanies { get; set; }

        private async Task CheckboxRowSelectHandler(RowSelectEventArgs<CompanyStub> args)
        {
            SelectedCompanies.Add(args.Data);

            await UpdateCompanies.InvokeAsync(SelectedCompanies);

        }

CheckboxRowSelectHandler does get called, but the UpdateCompanies event never fires.

I am expecting for the event to be fired, but the event never gets fired.

4
  • 1
    What is CompanyTable it's not child.razor ? Commented Jan 27, 2023 at 3:36
  • Where did you fire CheckboxRowSelectHandler? your code is not complete ! Commented Jan 27, 2023 at 7:44
  • @Nb777 It comes from a Syncfusion datagrid. That part of the code works. When a checkbox is clicked, that method does fire with the correct parameter. Commented Jan 27, 2023 at 13:32
  • Add Syncfusion as tag! Commented Jan 27, 2023 at 14:29

1 Answer 1

1

Here's a simplified version of your code that works. Use it to test your child/parent.

One thing to check: Is your razor.cs file correctly linked to the razor component file?

ChildComponent.razor

<div class="m-2 p-2 bg-light">
    <h3>ChildComponent</h3>

    <button class="btn btn-primary" @onclick=this.OnClick>Click me</button>

</div>

@code {
    [Parameter] public EventCallback<string> ValueChanged { get; set; }

    private async Task OnClick()
        => await this.ValueChanged.InvokeAsync($"Set at {DateTime.Now.ToLongTimeString()}");
}
@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<ChildComponent ValueChanged="@this.OnClicked" />

@if (!string.IsNullOrWhiteSpace(this.message))
{
    <div class="alert alert-info">
        @message
    </div>
}

@code {
    private string message = string.Empty;

    // No call to StateHasChanged required
    // The ComponentBase UI handler does it
    private void OnClicked(string value)
        => message = value;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.