Prepopulate dropdownlist with Selected Value

Hi,

Please how do you set the selected value on the dropdownlist after a page reload. 

eg from the url or setting the value oninitialised.

Selected Value does not set it in code below, even tho the if statement is true.

Please see code below

<SfDropDownList TValue="string" Placeholder="Groups" TItem="Groups" DataSource="@Group" @bind-Value="@SelectedValue">

    <DropDownListEvents TItem="Groups" TValue="string" ValueChange="@ValueChangeHandler" ></DropDownListEvents>

    <DropDownListFieldSettings Text="Text" Value="Value"></DropDownListFieldSettings>

</SfDropDownList>

@code {

    private string SelectedValue { get; set;}



    public class Groups

    {

        public string Text { get; set; }


        public string Value { get; set; }

    }


    List<Groups> Group = new List<Groups>

    {

        new Groups() { Value = "Group/EE", Text = "EE" },

        new Groups() { Value = "Group/Orange", Text = "Orange" },

        new Groups() { Value = "Group/T-mobile", Text = "T-mobile" },

    };


    private void ValueChangeHandler(ChangeEventArgs<string, Groups> args)

    {

        if(string.IsNullOrEmpty(SelectedValue) == false){

            NavigationManager.NavigateTo(SelectedValue, true);

        }

    }


    protected override void OnInitialized()

    {

        var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);


        if (uri.ToString().Contains("EE"))

        {

            SelectedValue = "EE";


        }


    }




}


1 Reply

SP Sureshkumar P Syncfusion Team March 4, 2022 07:46 AM UTC

Hi Andrew, 
 
We suggest you use our EnablePersistence property to maintain previously selected value after reloading the page in dropdownlist component.  Also, the ID property is needed to add when use the EnablePersistence property.  
 
Find the code example: 
 
<SfDropDownList ID="dllComponent" TValue="string" Placeholder="Groups" EnablePersistence="true" TItem="Groups" DataSource="@Group" @bind-Value="@SelectedValue"> 
 
    <DropDownListEvents TItem="Groups" TValue="string" ValueChange="@ValueChangeHandler" ></DropDownListEvents> 
 
    <DropDownListFieldSettings Text="Text" Value="Value"></DropDownListFieldSettings> 
 
</SfDropDownList> 
 
 
Regards, 
Sureshkumar P 


Loader.
Up arrow icon