JavaScript UI Libraries & Blazor Components Suite – Smart UI Forums DropDownList Get text of Selected Item in Drop Down List

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #108902
    salasidis
    Participant

    I am using Blazor Version

     

    the declaration in HTML is

    <DropDownList @ref=”EWDeviceList” SelectedIndexes=”selectedIndexes” OnChange=”ChangeDevice” />

     

    The code in @code is

    private async void ChangeDevice(Event chEv)
    {
    //ListBoxChangeEventDetail detail = chEv[“Detail”] as ListBoxChangeEventDetail;

    if (dpData != null)
    {
    dpData.Clear();
    }
    // add dev par data

    string devNm = EWDeviceList.SelectedValues[0].ToString();

    }

    The event always fire twice, and although the value is correct the first time, on firing again, it resets the drop down list to the first element. This does not happen if I remove the …SelectedValues[0]… part of the code.

     

    I looked for an example of OnChanged with the DropDownList on the Documentation, but could not find one.

     

    Thanks

    #108907

    Hi,

    This is happening because you have passed selectedIndexes.
    In the OnChange you should change the selected indexes to match the current index.

    Here is the fixed version:
    <DropDownList @ref=”EWDeviceList” SelectedIndexes=”@selectedIndexes” OnChange=”ChangeDevice” DataSource=”@dataSource”/>

    @code
    {
    DropDownList EWDeviceList = default!;

    string[] dataSource = new string[] { “One”, “Two” };

    int[] selectedIndexes = new int[] { 1 };

    private async void ChangeDevice(Event chEv)
    {
    DropDownListChangeEventDetail detail = chEv[“Detail”];

    selectedIndexes = new int[] { detail.Index };

    string devNm = EWDeviceList.SelectedValues[0].ToString();

    }
    }

    I hope this helps!

    Best Regards,
    Svetoslav Borislavov

    Smart UI Team
    https://www.htmlelements.com/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.