|
@using Syncfusion.Blazor.Inputs
<SfTextBox @bind-Value="@val"></SfTextBox>
<br />
<br />
<button @onclick="clickAction">Clear All</button>
@code{
public string val { get; set; } = "Hello World";
public void clickAction()
{
this.val = null;
}
} |
My situation is a bit different than that.
What I'm going is using the TextBox to allow users to enter commands to be processed. After I process them I want to clear the textbox for the next command. So I need to clear the input while inside the Input handler and/or the @onkeypress handler.
<SfTextBox @ref="@sfTextBox" @bind-Value="@NavString" Input="@NavInputHandler" @onkeypress="@KeyPressHandler"></SfTextBox>
private async void NavInputHandler(InputEventArgs args)
{
NavCurrentVal = args.Value;
// potentially process and clear input
}
private async Task KeyPressHandler(KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
// process NavCurrentVal and clear input
NavString
= null;
}
}
A more complete example. Press Z and the "Z pressed" message appears. The Z should be erased from the textbox but is not.
@page "/test"
@using Syncfusion.Blazor.Inputs
<h3>Test</h3>
@message
<SfTextBox @bind-Value="@val" @onkeypress="@KeyPressHandler" Input="@NavInputHandler" ></SfTextBox>
<br />
<br />
<button @onclick="clickAction">Clear All</button>
@code {
public string val { get; set; }
private string NavCurrentVal { get; set; }
private string message { get; set; }
public void clickAction()
{
this.val = null;
}
private async Task KeyPressHandler(KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
// process val and clear input
NavCurrentVal = null;
val = null;
}
}
private async void NavInputHandler(InputEventArgs args)
{
NavCurrentVal = args.Value;
switch (NavCurrentVal)
{
case "Z":
// do some work
message = "Z pressed";
NavCurrentVal = null;
val = null;
break;
default:
break;
}
}
}
|
@using Syncfusion.Blazor.Inputs
<h3>Test</h3>
@message
<SfTextBox @bind-Value="@val" @onkeyup="@KeyPressHandler" Input="NavInputHandler"></SfTextBox>
<br />
<br />
<button @onclick="clickAction">Clear All</button>
@code {
public string val { get; set; }
private string NavCurrentVal { get; set; }
private string message { get; set; }
public void clickAction()
{
this.val = null;
}
private async Task KeyPressHandler(KeyboardEventArgs args)
{
if (args.Key == "Enter" || args.Key == "Z")
{
val = null;
}
await Task.CompletedTask;
}
private async void NavInputHandler(InputEventArgs args)
{
if (args.Value == "Z")
{
val = args.Value;
}
await Task.CompletedTask;
}
} |
I open again this issue because I have a similar situation and what I read is not a solution for me.
I have 5 textboxes and in the first four textboxes I need to capture the text typed and when the customer click on "enter" I move the focus on the next textbox
I handle this succcessfully using this piece of code:
<SfTextBox @ref="centroref" @bind-Value="@SelectedData.CentroId" OnChange="OnChangeCentro" Width="50px" Placeholder="Rep.Id" FloatLabelType="FloatLabelType.Always"></SfTextBox>
public async Task OnChangeCentro(Microsoft.AspNetCore.Components.ChangeEventArgs args)
{
await oreref.FocusIn(); //Move focus to the next textbox
}
When I arrive to the last textbox, I need to capture the "enter" key because on that event I have to save all the data in the database so, in this textbox the customer is free to type chars if necessary or press only the Enter key.
To hande this I need to use the @onpresskey event but after pressing the enter key and saving the data in the database the textbox keeps surprisingly the old content even if I try to clear it in this way:
//Last textbox
<SfTextBox @ref="noteref" @bind-Value="@SelectedData.TtrapNote" @onkeypress="@OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
SelectedData.TtrapNote = notetext;
await Save(); //sava data in the DB
notetext = "";
noteref.Value = null;
await Task.CompletedTask;
}
else
notetext += args.Key;
}
I can't use the @Onkeyup approach because when I arrive to the last textbox, the "enter" key is the last key pressed and the OnInputNote event is immediately fired instead I'd need to wait for the next Enter key from the customer
How can I solve this ?
thanks
|
SfTextBox @ref="centroref" @bind-Value="@CentroId" ValueChange="OnChangeCentro" Width="50px" Placeholder="Rep.Id" FloatLabelType="FloatLabelType.Always"></SfTextBox>
<SfTextBox @ref="noteref" @bind-Value="@TtrapNote" ValueChange="@OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
@code {
SfTextBox centroref;
SfTextBox noteref;
public string CentroId { get; set; }
public string TtrapNote { get; set; }
public async Task OnChangeCentro(ChangedEventArgs args)
{
await noteref.FocusIn(); //Move focus to the next textbox
}
public void OnInputNote(ChangedEventArgs args)
{
var value = TtrapNote;
//if (args.Key == "Enter")
//{
// CentroId = TtrapNote;
// //await Save(); //sava data in the DB
// //notetext = "";
// //noteref.Value = null;
// await Task.CompletedTask;
//}
////else
// // notetext += args.Key;
}
} |
|
|
Thanks for this reply but when I arrive to the last textbox, the customer should be free to press enter leaving the textbox empty (and this will save the data in the DB) but in this case the ValueChange event is not fired
|
public void OnInputNote(ChangedEventArgs args)
{
//excecute once the enter button clicks
// args.IsInteracted returns false after set the two-way binding variable value as empty
if(args.IsInteracted)
{
var value = TtrapNote;
TtrapNote = "";
CentroId = "";
}
} |
Thanks for your reply but this didn't solve my case
I try to explain what is necessary to me using a sort of "if clause"
when the cursor is in the last textbox this is the code I need:
if ((textbox is empty || textbox is filled) && customer press enter)
{
save data in the DB
}
In your last piece of code, the function OnInputNote is not called in case the customer simply click on enter when the textbox is empty so I can't save data in the DB
|
<SfTextBox @ref="centroref" @bind-Value="@CentroId" ValueChange="OnChangeCentro" Width="50px" Placeholder="Rep.Id" FloatLabelType="FloatLabelType.Always"></SfTextBox>
<SfTextBox @ref="noteref" @bind-Value="@TtrapNote" ValueChange="@OnInputNote" @onkeypress="@OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
@code {
SfTextBox centroref;
SfTextBox noteref;
public Boolean checkChangeTrigger { get; set; } = false;
public string CentroId { get; set; }
public string TtrapNote { get; set; }
public async Task OnChangeCentro(ChangedEventArgs args)
{
await noteref.FocusIn(); //Move focus to the next textbox
}
public void OnInputNote(ChangedEventArgs args)
{
//excecute once the enter button clicks
// args.IsInteracted returns false after set the two-way binding variable value as empty
if (args.IsInteracted)
{
var value = TtrapNote;
TtrapNote = "";
CentroId = "";
checkChangeTrigger = true;
} else
{
checkChangeTrigger = false;
}
}
public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter" && string.IsNullOrEmpty(this.TtrapNote) && !checkChangeTrigger)
{
// excecute the function when textbox value is empty and press eneter key
// otherwise the textbox change event will trigger.
}
}
} |
public void OnInputNote(ChangedEventArgs args) is fired everytime I press any key in my side, not only when I press the Enter key and
args.IsInteracted is true so this code doesn't work
|
<SfTextBox @ref="noteref" @bind-Value="@SelectedData.TtrapNote" @onkeypress="@OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
SelectedData.TtrapNote = notetext;
await Save(); //sava data in the DB
notetext = "";
noteref.Value = null;
await Task.CompletedTask;
}
else
notetext += args.Key;
}
|
Probably there's still something not clear to me...
I'm using your code
<td>
<SfTextBox @ref="noteref" @bind-Value="@TtrapNote" ValueChange="@OnInputNote" @onkeypress="OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
</td>
public void OnInputNote(ChangedEventArgs args)
{
//excecute once the enter button clicks
// args.IsInteracted returns false after set the two-way binding variable value as empty
if (args.IsInteracted)
{
var value = TtrapNote;
TtrapNote = "";
checkChangeTrigger = true;
}
else
{
checkChangeTrigger = false;
}
}
public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter" && string.IsNullOrEmpty(this.TtrapNote) && !checkChangeTrigger)
{
// excecute the function when textbox value is empty and press eneter key
// otherwise the textbox change event will trigger.
SelectedData.TtrapNote = TtrapNote;
await Save();
}
}
but as soon as I click on any key, the event in bold public void OnInputNote(ChangedEventArgs args) is fired after the other event public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
The bind-value is matched to a variable "TtrapNote" that never changes... why the ValueChange is fired ?
Hello Sureshkumar
thanks for the great support, even sending a video, but the problem is still there
I'm using now your complete application and I placed 2 breakpoint where you can see in the picture
I'm in the first textbox where I typed some chars and then I clicked on enter key
the cursor is moved correctly to the second texbox
Here a typed letter 'a' and this is the sequence of events fired:
public async Task OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
then
public void OnInputNote(ChangedEventArgs args)
with args.IsInteracted = true
and then again
public void OnInputNote(ChangedEventArgs args)
with args.IsInteracted = false
everything perfect up to now I think (even if public void OnInputNote(ChangedEventArgs args) is fired without typing the enter key)
but now, if I click on enter key no events are fired so how can I start saving my data after typing some chars in the second textbox + enter key ?
Hi Walter,
Currently, we are validating your reported query. we will update you the further details in two business days on or before 3rd March 2022.
Regards,
Vinitha
|
@using Syncfusion.Blazor.Inputs
@using System.ComponentModel.DataAnnotations
<SfTextBox @ref="centroref" @bind-Value="@CentroId" @onkeyup="OnChangeCentro" Width="50px" Placeholder="Rep.Id" FloatLabelType="FloatLabelType.Always"></SfTextBox>
<SfTextBox @ref="noteref" @bind-Value="@TtrapNote" @onkeyup="OnInputNote" Placeholder="Note" FloatLabelType="FloatLabelType.Always"></SfTextBox>
@code {
SfTextBox centroref;
SfTextBox noteref;
public string CentroId { get; set; }
public string TtrapNote { get; set; }
public async Task OnChangeCentro(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
await noteref.FocusIn(); //Move focus to the next textbox
}
}
public async void OnInputNote(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
var value = TtrapNote;
// await Save(); //sava data in the DB
TtrapNote = "";
CentroId = "";
// we have called focusout because of the enter key we reset all the value from the application
await this.noteref.FocusOutAsync();
await Task.CompletedTask;
}
else
TtrapNote += args.Key;
}
} |