Blazor Server app: Why is input in an html table with embedded Input tags also showing in the next cell after hitting tab?

Steve Rehling 145 Reputation points
2023-07-04T17:19:57.5033333+00:00

I have a Blazor Server app built with Visual Studio Community 2022 v15.5.5. One component contains an html table with embedded input elements in each td cell, some showing user-entered data (via a value attribute and with CSS yellow highlighting) and others showing default values (via a placeholder attribute) where the user hasn't (yet) entered anything .

When I enter user data in a default cell that's followed by 2 more default cells in the same row, the entered value ends up formatted in the 1st cell (good) and appears unformatted in the 2nd cell (bad). Also, if I enter the data using the tab key, the cursor ends up in the 3rd cell, even though the onfocus events indicate the 2nd cell has focus. Finally, I've noticed since styling the cells with CSS that the surprise value in the 2nd cell is colored red. I'm not sure why that is.

I have put a demo app that shows the problem in Github (see here). The demo code is in the Counter page and the CSS is in site.css. There are Debug.Writeline statements that show what's happening with the underlying data that's rendered in the table. I'm guessing the problem is with the editing of the 2nd cell, but don't understand this enough to be sure. Samples of the user-data and default cell html appear below.

P.S. I am using the "value" versus "placeholder" approach instead of using "value" for both to get around the situation where the user enters a value that matches the default, in which case no OnChange event is triggered that will let me detect a user-entered value that happens to match the default, which I need to detect.

Any thoughts would be appreciated.

@if (bbdCell.CSSClass == enteredData)                         
{                         
<td class="@bbdCell.CSSClass" contenteditable="false" style="width: @colWidth">                             		   

<input style="width:@colWidth" class="@bbdCell.CSSClass"                                 value="@bbdCell.Value_Formatted" placeholder=""                                 
onFocus="this.select()"                                 
@onchange="@((ChangeEventArgs e) => OnChange(e, bbdCell.Value_Formatted, yearIndex, colIndex))"                                 @onfocusin="@((FocusEventArgs e) => OnFocusIn(e, yearIndex, colIndex))"                                 @onfocusout="@((FocusEventArgs e) => OnFocusOut(e, bbdCell.Value_Formatted, yearIndex, colIndex))" />                                             

</td>                         
}                     
else                         
{                         
<td class="@bbdCell.CSSClass" contenteditable="false" style="width: @colWidth">                             

<input style="width:@colWidth" class="@bbdCell.CSSClass"                                 
value = ""  placeholder="@bbdCell.Value_Formatted"                                 
onFocus="this.select()"                                 
@onchange="@((ChangeEventArgs e) => OnChange(e, bbdCell.Value_Formatted, yearIndex, colIndex))"                                 @onfocusin="@((FocusEventArgs e) => OnFocusIn(e, yearIndex, colIndex))"                                 @onfocusout="@((FocusEventArgs e) => OnFocusOut(e, bbdCell.Value_Formatted, yearIndex, colIndex))" />

</td>                         
}
Developer technologies .NET Blazor
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.