question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to prevent OnSelectedItemChanged Call after Refresh

See original GitHub issue

Hi,

Each time the page is reloaded or clicked, the OnSelectedItemChanged for Select is executed. How can I prevent this? The same behavior can be seen on the Ant Blazor Page. The OnSelectedItemChanged should only be called, if I select an Item

<Select @bind-Value="@context.ColorState" 
   	DefaultValue = "@context.ColorState"
   	TItemValue="ColorState" 
   	TItem="ColorState"
   	OnSelectedItemChanged="(value)=>UpdateColor(context)">
   <SelectOptions>
   	@foreach (ColorState color in Enum.GetValues(typeof(ColorState)))
   	{
   		<SelectOption TItemValue="ColorState" TItem="ColorState" Value="@color" />
   	}   
   </SelectOptions>
</Select>  

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
anranruyecommented, Oct 27, 2021

@anddrzejb

I agree with @minas15 , and this is not the only issue with OnSelectedItemChanged. When set the Value programmatically, OnSelectedItemChanged is not triggered.

And I basicly agree with @susahin80 .

there might be a scenario when value is changed by some external service

Microsoft suggests that paramter properties should only be set by binding values. However, it’s true that we don’t always follow this rule. I think we should act base on the source of Value when it changes. If the Value is from the binding Value then we should not trigger ValueChanged, otherwise there will be an unnecessary rerendering.

0reactions
anddrzejbcommented, Oct 30, 2021

@anranruye thank you for sharing the issue. It was a worthwhile read. And I agree with your understanding of properties use when marked with [Parameter] attribute. Although I am not sure if the suggestion of forcing only {get;set;} is entirely warranted. Of course, without understanding of possible underlying problems, it is a good solution, but in the hands of a programmer well versed in blazor internals, using setters (or getters) can help keeping the code cleaner & leaner.

But now I can see that there was a misunderstanding (or at least I think there was). I was not referring to changing value of the child component (so the [Parameter] Value {get;set;}, I was rather referring to changing the parent’s variable that is bound to Value parameter. But the more thought I gave to the issue, the more I think @susahin80 is actually correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid page refresh after selectedindexchanged of ...
I am using update panel and ASP drop down. When I select any value from a drop down list I load some data...
Read more >
How to Keep selecteditem when refresh ItemSource.
Hello, I'm try to keep selecteditem in my GridControls after refresh grid itemsource property. I have two kinds of itemsource.
Read more >
Avoid Prevent Page refresh PostBack after ...
Here Mudassar Khan has explained with an example, how to avoid (Prevent) Page refresh (reload) after SelectedIndexChanged is fired in ASP.
Read more >
Select previously selected row after refresh in UI for WPF
Hello Vikas, I am attaching a sample application demonstrating how the SelectedItem property of RadGridView can be set. For this purpose, I have ......
Read more >
How can I prevent SelectionChanged event for a ...
Actually, I have some method, and I called this method in Combobox Drop down event and that causing the item source to reset....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found