Skip to content

Detect query string value change in component in blazor web assembly. #28457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TanvirArjel opened this issue Dec 6, 2020 · 4 comments
Closed
Assignees
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@TanvirArjel
Copy link
Contributor

TanvirArjel commented Dec 6, 2020

For say, I have the following URL for the product page:

https://localhost:44389/products

If it is changed to https://localhost:44389/products?pageIndex=2 on pagination click, is there any way to detect this change from Component partial class without onclick event on pagination anchor tag?

I have search a lot but unfortunately did not find anything.

Thank you.

I am expecting something as follows:

public class partial Products
{
    protected override async Task OnQueryStringChangeAsync()
    {
             ............
    }
}
@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly labels Dec 7, 2020
@mrpmorris
Copy link

There is an event you can subscribe to on NavigationManager if you inject that.

@TanvirArjel
Copy link
Contributor Author

@mrpmorris If possible please write some example code.

@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us. This will also be simplified by #22388

@captainsafia
Copy link
Member

@TanvirArjel You can leverage the LocationChanged API on the NavigationManager as mentioned above. You can use the following code in your component to achieve your goal:

@code {
    protected override void OnInitialized()
    {
        Navigation.LocationChanged += NavigationManager_LocationChanged;
    }

    private void NavigationManager_LocationChanged(object sender, LocationChangedEventArgs e)
    {
        var url = e.Location;
        // Parse URL for query string here
    }

    void IDisposable.Dispose() => Navigation.LocationChanged -= NavigationManager_LocationChanged;
}

The issue linked above tracks making it easier to extract query strings from the URL. For now, you can manually parse it as indicated above.

@captainsafia captainsafia added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved labels Dec 7, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants