How to pass more information when checked change

Hi all, i have a lot of switch buttons created inside a foreach loop, now I need to do execute some custom code when the switch button change the value, I'm trying to use ValueChange and it work, I can send the custom information that i need but i cannot determine if the button is checked or not (i'm creating a new ChangeEventArgs object)

My code:

@foreach (var option in module.Options)
{
    <SfSwitch ValueChange="@(() => OnChangeButton(new Syncfusion.Blazor.Buttons.ChangeEventArgs(), option))" ID="@option.Id" Checked="false"></SfSwitch>   
}

public void OnChangeButton(Syncfusion.Blazor.Buttons.ChangeEventArgs e, ModuleOptionDto option)
{
    // Here, e.checked is always false
}

Any idea on this?
Thanks,

5 Replies

SD Saranya Dhayalan Syncfusion Team May 4, 2020 06:22 AM UTC

Hi Julio, 
 
Thank you for contacting Syncfusion support 
 
We have checked your reported issue, we are not able to reproduce it in our end in 18.1.0.46 version. In ValueChange event e.checked return correct value. Please find the below code snippet: 
 
@using Syncfusion.Blazor.Buttons 
 
 
    <SfSwitch ValueChange="OnChangeButton" Checked="false">SfSwitch> 
 
 
@code{ 
 
    public void OnChangeButton(Syncfusion.Blazor.Buttons.ChangeEventArgs e) 
    { 
        if(e.Checked == true) 
        { 
            Console.WriteLine("true"); 
        } 
    } 
 
 
} 
 
Please find the below screenshot: 
 
 
 
Please check your issue with the above code snippets, if the issue persists,  
 
1.       If possible, please share your issue reproducible sample.  
2.       Please share the Syncfusion.Blazor dll version details.  
 
  
Could you please provide the above requested information, based on that we will check and provide you a better solution quickly. 
 
Regards, 
Saranya D 



JA julio Avellaneda May 4, 2020 01:42 PM UTC

Hi Sarany, thank you for your help, your example works, but my case is a little bit different, I want to pass more information to that event, because I'm generating some switch controls inside a foreach, i need to read if the switch is checked or not and also read the custom information that i need.

Foreach code: I'm generating a switch control for each item and I'm trying to pass the option object to the ValueChange event:

@foreach (var option in module.Options)
{
    <SfSwitch ValueChange="@(() => OnChangeButton(new Syncfusion.Blazor.Buttons.ChangeEventArgs(), option))" ID="@option.Id" Checked="false"></SfSwitch>   
}

This the OnChangeButton code, as you case, the second parameter is the object that i need, but in this case, e.checked is always false:
public void OnChangeButton(Syncfusion.Blazor.Buttons.ChangeEventArgs e, ModuleOptionDto option)
{
    // Here, e.checked is always false
}

Let me know if now it's more clear, if not, I can prepare a small example.



SD Saranya Dhayalan Syncfusion Team May 5, 2020 05:11 PM UTC

Hi Julio,  
 
We have checked your reported query, you can resolve this by providing proper arguments in valueChange event. Please find the below code snippet: 
 
@using Syncfusion.Blazor.Buttons 
@using System.ComponentModel.DataAnnotations 
 
@foreach (ModuleOptionDto option in module) 
{ 
    <SfSwitch ValueChange="@((Syncfusion.Blazor.Buttons.ChangeEventArgs e) => OnChangeButton(e, option))" ID="@option.Id" Checked="false"></SfSwitch> 
} 
 
 
@code{ 
 
    public void OnChangeButton(Syncfusion.Blazor.Buttons.ChangeEventArgs e, ModuleOptionDto option) 
    { 
        // Here, e.checked is always false 
    } 
    List<ModuleOptionDto> module = new List<ModuleOptionDto> { 
        new ModuleOptionDto { Id="a1", Name="Switch1" }, 
        new ModuleOptionDto { Id="a2", Name="Switch1" } 
 
    }; 
    public class ModuleOptionDto 
    { 
        public string Id { get; set; } 
        public string Name { get; set; } 
    } 
} 
 
 
Screenshot: 
 
 
Please check the above code snippet and get back to us if you need further assistance on this. 
 
Regards, 
Saranya D 



JA julio Avellaneda May 29, 2020 01:31 PM UTC

Saranya thank you, it works!


SD Saranya Dhayalan Syncfusion Team May 29, 2020 01:34 PM UTC

Hi Julio, 
 
Most Welcome… 
 
We are happy to hear that your requirement has been fulfilled. Kindly get back to us if you need any further assistance. 
 
Regards, 
Saranya D 


Loader.
Up arrow icon