We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to set focus to a numeric textbox from dropdownlist onvaluechange event

Hello !

Please tell me how to set focus to a numeric textbox from dropdownlist onvaluechange event.

After I select a dropdownlist value, I want to set the focus to a specific Syncfusyon texbox or Syncfusyon  numerictextbox.

Thank You !

Here is my code :

<SfDropDownList TValue="string" TItem="Country" Placeholder="Select a country" AllowFiltering="true" DataSource="@Countries" Width="300px">

    <DropDownListFieldSettings

        Text="Name"

        Value="Code">

    </DropDownListFieldSettings>

    <!--

    <DropDownListEvents TItem="Country" TValue="string"

          OnValueSelect="@DDL_Search_OnValueSelecthandler"

          ValueChange="@DDL_Search_ValueChangeHandler" >

    </DropDownListEvents>

    -->

</SfDropDownList>


<div>

    <SfNumericTextBox TValue="int?" Value=@NumericValue></SfNumericTextBox>

</div>


@code{

    public int? NumericValue { get; set; } = 5;

    public class Country

    {

        public string Name { get; set; }

        public string Code { get; set; }

    }


    private List<Country> Countries = new List<Country>

    {

        new Country() { Name = "Australia", Code = "4208" },

        new Country() { Name = "Bermuda", Code = "BM" },

        new Country() { Name = "Canada", Code = "CA" },

        new Country() { Name = "Cameroon", Code = "CM" },

        new Country() { Name = "Denmark", Code = "DK" },

        new Country() { Name = "France", Code = "FR" },

        new Country() { Name = "Finland", Code = "FI" },

        new Country() { Name = "Germany", Code = "4209" },

        new Country() { Name = "Greenland", Code = "GL" },

        new Country() { Name = "Hong Kong", Code = "HK" },

        new Country() { Name = "India", Code = "IN" },

        new Country() { Name = "Italy", Code = "IT" },

        new Country() { Name = "Japan", Code = "JP" },

        new Country() { Name = "Mexico", Code = "MX" },

        new Country() { Name = "Norway", Code = "NO" },

        new Country() { Name = "Poland", Code = "PL" },

        new Country() { Name = "Switzerland", Code = "CH" },

        new Country() { Name = "United Kingdom", Code = "GB" },

        new Country() { Name = "United States", Code = "4210" },

    };


    private void DDL_Search_OnValueSelecthandler(SelectEventArgs<ArticleForDDLModel> args)

    {

        // Here, you can customize your code.

    }


    private void DDL_Search_ValueChangeHandler(ChangeEventArgs<string, ArticleForDDLModel> args)

    {

        // Here, you can customize your code.

    }

}


7 Replies 1 reply marked as answer

UD UdhayaKumar Duraisamy Syncfusion Team April 17, 2023 02:28 PM UTC

Based on your requirements, you can use the "ValueChange" event of the Syncfusion dropdownlist control to set focus to a specific Syncfusion numeric textbox control. We have provided a code snippet for this below, which includes a dropdownlist control, a numeric textbox control, and event handlers for "OnValueSelect" and "ValueChange" events.


<div style="margin:130px auto;width:300px">

    <SfDropDownList TValue="string" TItem="Country" Placeholder="Select a country" AllowFiltering="true" DataSource="@Countries" Width="300px">

        <DropDownListFieldSettings Text="Name"

                                   Value="Code">

        </DropDownListFieldSettings>

        <DropDownListEvents TItem="Country" TValue="string"

                            OnValueSelect="@DDL_Search_OnValueSelecthandler"

                            ValueChange="@DDL_Search_ValueChangeHandler">

        </DropDownListEvents>

    </SfDropDownList>

    <hr />

    <div>

        <SfNumericTextBox @ref="NumericObj" TValue="int?" Value=@NumericValue></SfNumericTextBox>

    </div>

</div>

 

@code {

    public SfNumericTextBox<int?> NumericObj;

    public int? NumericValue { get; set; } = 5;

    public class Country

    {

        public string Name { get; set; }

        public string Code { get; set; }

    }

 

    private List<Country> Countries = new List<Country>

    {

        new Country() { Name = "Australia", Code = "4208" },

        new Country() { Name = "Bermuda", Code = "BM" },

        new Country() { Name = "Focus NumericTextBox", Code = "FN" },

    };

 

    private async Task DDL_Search_ValueChangeHandler(ChangeEventArgs<string, Country> args)

    {

        if (args.Value == "FN")

        {

            await NumericObj.FocusAsync();

        }

    }

}


In the above code, the "ValueChange" event handler is used to check if the selected value of the dropdownlist is equal to a specific value, which is "Focus NumericTextBox". If this condition is met, the "FocusAsync()" method of the numeric textbox control is called to set focus to it.



TJ Tom Johnes April 18, 2023 11:04 PM UTC

Sorry, but it is not working !

The focus always remains on the dropdownlist...

I am using Syncfusion Blazor Components 21.1.39



SP Sureshkumar P Syncfusion Team April 19, 2023 09:14 AM UTC

Tom,


Based on our previous updated code example, we have prepared the sample and attached it here in the attachment. we suggest you refer to the attached sample to achieve your requirement.


Regards,

Sureshkumar P


Attachment: DDLServer_f08063.zip


TJ Tom Johnes April 20, 2023 07:16 PM UTC

Your sample is working fine but your page doesn't work into my app.

I copied your entire page Index.razor from your sample into my project but IT IS NOT WORKING ! I am developing a Blazor Webassembly Application, not a Blazor Server like your sample is.

The focus always remains on the dropdownlist...

I am using Syncfusion Blazor Components 21.1.39

Thank You !



UD UdhayaKumar Duraisamy Syncfusion Team April 26, 2023 03:39 PM UTC

The issue you mentioned may be caused by the lack of an asynchronous delay between value changes, focus out events, and focus events. However, you can easily resolve this issue by introducing a delay in the change handler code. We have updated the solution code in our previous response to include a 1 millisecond delay using the Task.Delay() method.


private async Task DDL_Search_ValueChangeHandler(ChangeEventArgs<string, Country> args)

    {

        if (args.Value == "FN")

        {

            await Task.WhenAll(Task.Delay(1), ddlObj.FocusOutAsync(), NumericObj.FocusAsync());

        }

    }


Additionally, we have prepared a sample and provided it below for your reference.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Webassembly844509663


Marked as answer

TJ Tom Johnes April 28, 2023 01:17 AM UTC

Now it is working fine !

Thank You Very Much For Your Help !

Best Regards !



UD UdhayaKumar Duraisamy Syncfusion Team April 28, 2023 04:53 AM UTC

Glad the solution worked for you! Let us know if you have any further questions or need further assistance.


Loader.
Up arrow icon