Changing the value of an Input box from code

Baruch Barness 40 Reputation points
2023-07-08T20:46:56.85+00:00

I have an Input box that if user type a '0' I need to change so it to display a '1'. In the 'onChange' event I check the value entered and if '0', I set the value of @someVar (where @someVar is the value set on the input box (ie: 'value=@someVar') to '1' but input box value is not showing (although it is set correctly and the code works fine). Any advise of how to make the new value display in the input box?

Developer technologies .NET Blazor
Developer technologies Visual Studio Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-07-09T11:57:09.0566667+00:00

    I can't see your code so I have no idea how you code works. The example below is based on the official docs. The docs also how onchange works.

    ASP.NET Core Blazor data binding

    @page "/onchange"
    
    <PageTitle>OnChange</PageTitle>
    
    <h1>OnChange</h1>
    
    <input type="text" @bind:get="text" @bind:set="Set" />
    
    @code {
        private string text = "";
    
        private void Set(string value)
        {
            if (value == "0") {
                text = "1";
                return;
            }
            text = value;
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Baruch Barness 40 Reputation points
    2023-07-09T15:25:30.82+00:00

    Here is my code before and after the changes you had suggested. Can you advise why the errors indicated by the arrows?

    • Before

    Before

    • After

    After


  2. Baruch Barness 40 Reputation points
    2023-07-09T23:15:37.8766667+00:00

    I installed Dot Net 7.0 which support the 'bind:set..' method but now I get

    Error CS0117 'RuntimeHelpers' does not contain a definition for 'CreateInferredBindSetter'

    Which Microsoft says:

    Compiler Error CS0117

    'type' does not contain a definition for 'identifier'

    • This error occurs in some situations when a reference is made to a member that does not exist for the data type.

    Can you tell why?


  3. Baruch Barness 40 Reputation points
    2023-07-10T15:04:14.6466667+00:00

    Please see images below. I can send you the entire project if you tell me how. It is just a small modification of a Microsoft sample created when I first launched VS2023.

    Target Framework

    cproj


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.