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

Clear Text

Hi, how do I clear the control form code?

3 Replies

GG Gopi Govindasamy Syncfusion Team August 28, 2019 10:08 AM UTC

Hi Raul,  

As per our understanding, you expect to clear value from code. If you need to clear the value dynamically from code, use Value property and set it as null.  

Please find the code snippet for your reference.  

@using Syncfusion.EJ2.Blazor.DropDowns 
 
<div> 
    <button @onclick="@OnClick">Clear Text</button> 
</div> 
<EjsDropDownList @bind-Value="@selectCountry" ShowClearButton="true" DataSource="@Country"> 
    <DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings> 
</EjsDropDownList> 
 
@code{ 
 
    private string selectCountry;  
 
    public void OnClick(UIMouseEventArgs args) 
    { 
        this.selectCountry = null; 
    } 
 
   public class Countries 
    { 
        public string Name { get; set; } 
 
        public string Code { get; set; } 
    } 
 
    List<Countries> Country = new List<Countries> 
{ 
        new Countries() { Name = "Australia", Code = "AU" }, 
        new Countries() { Name = "Bermuda", Code = "BM" }, 
        new Countries() { Name = "Canada", Code = "CA" }, 
        new Countries() { Name = "Cameroon", Code = "CM" }, 
    }; 
} 


If you want clear control value from UI, we have provided “ShowClearButton” property to show the clear button that reset value, text, and index properties. 

Regards, 
Gopi G. 



RA Raul August 28, 2019 07:32 PM UTC

I had already tried it that way, my value is an int, and I have a property:
    private int? vehicleBrandId;

    public int? VehicleBrandId
    {
        get { return vehicleBrandId; }
        set
        {
            vehicleBrandId = value;
            this.StateHasChanged();
        }
    }

and the control:
            <EjsDropDownList ID="brands" PlaceHolder="Select the brand" @bind-Value="VehicleBrandId" DataSource="@VehicleBrands">
                <DropDownListEvents TValue="TanCoLogisticsSDK.VehicleBrand" OnValueSelect="onSelectBrand"></DropDownListEvents>
                <DropDownListFieldSettings Text="BrandName" Value="VehicleBrandID"></DropDownListFieldSettings>
            </EjsDropDownList>

then to test on a click of a button I set 
this.VehicleBrandId = null;
but the droplist still shows the selected brand, instead of clear it self like when the control is first rendered.


GG Gopi Govindasamy Syncfusion Team August 29, 2019 09:22 AM UTC

Hi Raul,  

Thanks for your update. 

We have validated your reported issue with shared code snippet. You have used incorrect datatype to TValue into DropDownListEvent. You can set the datatypes of Value property into TValue (TValue="int?"). We have prepared sample based on shared code snippet. Please find the sample and code snippet for your reference.  

<div> 
    <button @onclick="@OnClick">Clear Text</button> 
</div> 
 
<EjsDropDownList ID="brands" @ref:suppressField @ref="DropDownObj" Placeholder="Select the brand" @bind-Value="@VehicleBrandId" DataSource="@VehicleBrands"> 
    <DropDownListEvents TValue="int?" OnValueSelect="onSelectBrand"></DropDownListEvents> 
    <DropDownListFieldSettings Text="BrandName" Value="VehicleBrandID"></DropDownListFieldSettings> 
</EjsDropDownList> 
 
 
@code{  
 
    EjsDropDownList<int?> DropDownObj; 
      
    public int? VehicleBrandId 
    { 
        get { return vehicleBrandId; } 
        set 
        { 
            vehicleBrandId = value; 
        } 
    } 
    protected int? vehicleBrandId { get; set; } = 2; 
 
    public class Vehicles 
    { 
        public string BrandName { get; set; } 
 
        public int VehicleBrandID { get; set; } 
    } 
 
 
    public void OnClick(UIMouseEventArgs args) 
    { 
        this.VehicleBrandId = null; 
        this.DropDownObj.Text = null; 
        this.DropDownObj.Index = null; 
    } 


Regards,
Gopi G. 


Loader.
Live Chat Icon For mobile
Up arrow icon