Figure out which button was clicked

Hi there,

Suppose I have a list box with a button in each row:

<SfListBox @ref="listBoxObj" TValue="string[]" DataSource="@Items" TItem="ItemModel" >
<ListBoxFieldSettings Text="Name" Value="Id" />
<ListBoxTemplates TItem="ItemModel">
<ItemTemplate>
<div>@((context as ItemModel).Name)</div>
<SfButton OnClick="OnItemDelete" IconCss="fa fa-trash"></SfButton>
</ItemTemplate>
</ListBoxTemplates>
</SfListBox>

How do I know which row's button was clicked in the OnItemDelete() handler?

Thanks
Joseph



1 Reply 1 reply marked as answer

MK Mohan Kumar Ramasamy Syncfusion Team September 2, 2020 10:51 AM UTC

Hi Joseph, 
 
We have checked your reported query. We can achieve your requirement using lambda expressions. Please find the below code snippet for reference.  
 
@using Syncfusion.Blazor.DropDowns 
@using Syncfusion.Blazor.Buttons 
 
    <SfListBox TValue="string[]" DataSource="@Data" TItem="DataValues"> 
        <ListBoxFieldSettings Text="Name" Value="Id" /> 
        <ListBoxTemplates TItem="DataValues"> 
            <ItemTemplate> 
                @{  
                    var data = context as DataValues; 
                                <div style="float: left;">@data.text</div> 
                <SfButton OnClick="@(() => OnItemDelete(@data.text))" Content="Click"></SfButton> 
                    } 
            </ItemTemplate> 
        </ListBoxTemplates> 
    </SfListBox> 
 
@code { 
 
    private void OnItemDelete(string SelectedItem) 
    { 
 
    } 
 
 
For your reference, we have prepared a sample based on this. Please refer below link. 
 
 
Please let us know, if you need any further assistance. 
 
Regards, 
Mohankumar R 


Marked as answer
Loader.
Up arrow icon