parametersblazoronchange

How to pass multiple parameters on onchange in blazor


Is it possible to pass multiple parameters to onchange event in blazor?. If it is not possible, may I know what are my options. Thank you.

Requirement: User should have the ability to set color of their choice for each item. So, I need to pass additional parameter (itemNo) during each color selection.

Tried to pass extra parameter, but does not compile, throws error:

 <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B"  @onchange="((ChangedEventArgs args) => HandleColorChange(args, itemNo))">

 void HandleColorChange(ChangeEventArgs args, int itemNo)
 {
    colorSelected = args.Value.ToString();         
   //I need to set color as below for one specific item
     item[itemNo] = colorSelected;          
 }

Current working Code, if no extra parameter passed:

  <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B" @onchange="HandleColorChange">

  void HandleColorChange(ChangeEventArgs args)
  {
     colorSelected = args.Value.ToString();            
     //But I need to set this color for one specific item, for which I need to pass another parameter          
  }

Solution

  • Here you have working example:

    @for(int i=0; i < item.Length; i++)
    {
        int b = i;
        <div>
            <span>Selector for @i :</span>  <input bind=@colorSelected id="changeColor" type="color" value="#55BF3B"  @onchange="(args => HandleColorChange(args, b))">
        </div>
    }
    
    @for(int i=0; i < item.Length; i++)
    {
        <div>
            <span>Selected for @i :</span> @item[i]
        </div>
    }
    
    @code
     {  
         string colorSelected = "blue";
    
         string [] item = new string[10];
    
        void HandleColorChange(ChangeEventArgs args, int itemNo)
        {
            colorSelected = args.Value.ToString();         
            item[itemNo] = colorSelected;     
        }
     }
    

    https://blazorrepl.telerik.com/QxEhlQle42JJ49dz28