How to get reference to TelerikListView item during a TelerikTextBox ValueChanged call back?

1 Answer 54 Views
ListView TextBox
Will
Top achievements
Rank 1
Will asked on 13 Jun 2023, 01:29 PM

Blazor .Net 7

I have the following TelerikListView.  When the user changes the TelerikTextBox Cidr value I want to handle the ValueChanged event however when I do this, I need to update the underlying model object manually.  I am stuck.  When the TelerikListView is in edit mode, either for a new element or for an existing element I can't figureout how to reference the element correctly in the ValueChanged event handler.  I know I need to update the model manually but I am not sure how.  Any hints?

<TelerikListView Data="@SubnetList" Width="700px" Pageable="true"

                 OnCreate="@CreateHandler" OnDelete="@DeleteHandler" OnUpdate="@UpdateHandler"
                 OnEdit="@EditHandler" OnCancel="@CancelHandler">
    <HeaderTemplate>
        <h2>Subnet List</h2>
        <ListViewCommandButton Command="Add" Icon="@FontIcon.Plus">Add Subnet</ListViewCommandButton>
    </HeaderTemplate>
    <Template>
        <div class="listview-item">
            <h4>@context.Cidr</h4>
            <ListViewCommandButton Command="Edit" Icon="@FontIcon.Pencil">Edit</ListViewCommandButton>
            <ListViewCommandButton Command="Delete" Icon="@FontIcon.Trash">Delete</ListViewCommandButton>
        </div>
    </Template>
    <EditTemplate>
        <div style="border: 1px solid green; margin: 10px; padding: 10px; display: inline-block;">
            <TelerikFloatingLabel Text="CIDR">
                <TelerikTextBox Id="Cidr" ValueChanged="@CidrValueChangedHandler" />
            </TelerikFloatingLabel><br />
            <ListViewCommandButton Command="Save" Icon="@FontIcon.Save">Save</ListViewCommandButton>
            <ListViewCommandButton Command="Cancel" Icon="@FontIcon.Cancel">Cancel</ListViewCommandButton>
        </div>
    </EditTemplate>
</TelerikListView>
@code {
    private List<Models.Subnet> SubnetList = new List<Models.Subnet>();

    protected override async Task OnInitializedAsync() 
        => SubnetList = await SubnetService.GetAllAsync();

    private void CidrValueChangedHandler(string theUserInput)
    {
        // manually update the model here

// calculate subnet properties here to help the user select size the subnet
// correctly by selecting the number of bits in the network side of the mask
// # number of hosts, etc.
    }

    async Task GetListViewData()
        => SubnetList = await SubnetService.GetAllAsync();
}

1 Answer, 1 is accepted

Sort by
0
Yanislav
Telerik team
answered on 16 Jun 2023, 10:27 AM

Hello Will,

Through the EditTemplate context, you can access the edited model. Then if you need to pass the model to the ValueChanged handler you can declare it as a lambda expression. 

<TelerikListView Data="@ListViewData" Pageable="true" PageSize="15" OnUpdate="@UpdateHandler">
    <EditTemplate>
        @{
            var sample = context as SampleData;
        }
        <TelerikTextBox Id="Cidr" Value="@context.Name" ValueChanged="@((string theUserInput) => CidrValueChangedHandler(theUserInput, sample))" />

Here is a REPL example that demonstrates the approach: https://blazorrepl.telerik.com/GxOgvUuW00dFJx7Q11

I hope this helps. 

Regards,
Yanislav
Progress Telerik

As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.
Tags
ListView TextBox
Asked by
Will
Top achievements
Rank 1
Answers by
Yanislav
Telerik team
Share this question
or