Skip to content

@bind="" and @oninput="" issue Blazor #17702

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
faisal5170 opened this issue Dec 9, 2019 · 2 comments
Closed

@bind="" and @oninput="" issue Blazor #17702

faisal5170 opened this issue Dec 9, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@faisal5170
Copy link

faisal5170 commented Dec 9, 2019

below is my HTML content

<input type="text" id="txtSearch" placeholder="Search Titles..." class="form-control" @Bind="searchTerm" @oninput="@FilterRecords" />

@code {
private string searchTerm;

public async Task FilterRecords(EventArgs args)
{
//code
}

but when I press key searchTerm property get previous values like If I press "a" it will take null, if I press "ab" it will take "a", press "abc" will take "ab" etc..
}

@SteveSandersonMS
Copy link
Member

Yes, this is because the oninput event runs before onchange, so your FilterRecords method runs before the @bind handler has run. By default, @bind attaches to the onchange event.

As an alternative, consider removing oninput, and instead use @bind with @bind:event="oninput". Then you can put arbitrary logic to react to updates inside the setter of whatever property you're writing to. This is simpler because then you only have one event handler to deal with.

@SteveSandersonMS SteveSandersonMS added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Dec 9, 2019
@ghost ghost added the Status: Resolved label Dec 9, 2019
@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Dec 9, 2019
@faisal5170
Copy link
Author

Thanks, it works, I'm adding a solution below so it will be helpful for others.

<input type="text" id="txtSearch" placeholder="Search Titles..." class="form-control" @Bind="SearchTerm" @Bind:event="oninput" />

@code {
private string searchTerm;
private string SearchTerm
{
get { return searchTerm; }
set { searchTerm = value; FilterRecords(); }
}

public void FilterRecords()
{
    //code
}

}

@ghost ghost locked as resolved and limited conversation to collaborators Jan 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ 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

3 participants