0

I have 2 pages that have the following and both pages in their code behind have their own Search Method.

<button class="btn input-search-btn no-outline" @onclick="@(async () => await Search())">

so What I want to do is place this in it's own component and when the user clicks the button that the component raises a 'search' event and then each page using this component to implements it's own search. (there is more to the component, but this is my basic issue)

1 Answer 1

5

Something like this:

SearchComponent.razor

<button class="btn input-search-btn no-outline" @onclick="@RequestSearch">

@code
{
    [Parameter]
    public EventCallback OnSearchRequested {get;set;}

    async Task RequestSearch()
    {
      await OnSearchRequested.InvokeAsync();
    }
}

PageA.razor

<SearchComponent @OnSearchRequested="SearchA" />

@code
{
   async Task SearchA()
   {
      // Page A search logic

   }

}

PageB.razor

<SearchComponent @OnSearchRequested="SearchB" />

@code
{
   async Task SearchB()
   {
      // Page B search logic

   }

}

Note that you can bind search parameters (string, options etc say SearchParams) in SearchComponent and raise EventCallback<SearchParams> instead.

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.