<EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<SfTextBox ID="EmpID" @bind-Value="@employee.EmpID" @onkeydown="@keydown"></SfTextBox>
</div>
<div class="form-group">
<SfTextBox ID="EmpName" @bind-Value="@employee.EmpName" @onkeydown="@keydown"></SfTextBox>
</div>
<div class="form-group">
<SfTextBox ID="EmpAddress" @bind-Value="@employee.EmpAddress" @onkeydown="@keydown"></SfTextBox>
</div>
<div class="form-group">
<SfComboBox TValue="string" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @onkeypress="@keydown">
<ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
</SfComboBox>
</div>
<div class="form-group">
<SfComboBox TValue="string" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @onkeypress="@keydown">
<ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
</SfComboBox>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</EditForm>
@code{
[Inject]
protected IJSRuntime JsRuntime { get; set; }
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
JsRuntime.InvokeVoidAsync("onFocus", "EmpID");
}
}
}
|
window.onFocus = (id) => {
var currInput = document.activeElement;
if (currInput.tagName.toLowerCase() == "input") {
var inputs = document.getElementsByTagName("input");
var currInput = document.activeElement;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i] == currInput) {
var next = inputs[i + 1];
if (next && next.focus) {
next.focus();
}
break;
}
}
}
} |
Sorry for late reply
I have two clarification
1.Can we handle model validation if I use enter key navigation (because default submit behavior is enter key)
2.Is it possible to write dynamic code for focusing input control whe I press enter key
First example
One code for autofocus for all control
No need to define field name like below code
<SfTextBox @ref="textobj" ID="EmpID" @bind-Value="@employee.EmpID" @onkeydown="@keydown"></SfTextBox>
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
this.textobj.FocusIn();
}
} |
I have one more question in keyboard navigation.
The above code is working fine as per our requirement. ( static control)
I would like to know how to handle keyboard (enter key) navigation for generic object ( dynamic control ,it can be dynamic control from generic object)
I have below genereic source code.I need to know how to use tab index when i press enter key.
Note:-
I example how to call gereric field name (EmpId) in "JsRuntime.InvokeVoidAsync"
Example Source Code :-
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
JsRuntime.InvokeVoidAsync("onFocus", "EmpID");
}
}
Genereic Source Code :-
<div class="editForm">
<div class="row ml-md-2">
@if (genericPageFilters != null)
{
@foreach (var filterType in genericPageFilters)
{
var data = Enum.GetName(typeof(ControlName), Convert.ToInt32(@filterType.ControlType));
var Datasource = filterType.DataSource;
var placeHolder = filterType.WaterMark;
var isMandatory = filterType.IsMandatory;
var isDisabled = filterType.IsDisabled;
var Filterkey = filterType.FilterName;
var isVisible = filterType.IsVisible;
var maxLengthAttr = filterType.MaxLength;
bool disable = (isDisabled == 0 ? true : false);
var visible = "block";
if (isVisible == 0)
visible = "none";
var maxLength = commonHtmlAttribute.FifteenHtmlAttribute;
if (maxLengthAttr == "FiveHtmlAttribute")
maxLength = commonHtmlAttribute.FiveHtmlAttribute;
else if (maxLengthAttr == "TenHtmlAttribute")
maxLength = commonHtmlAttribute.TenHtmlAttribute;
else if (maxLengthAttr == "FifteenHtmlAttribute")
maxLength = commonHtmlAttribute.FifteenHtmlAttribute;
else if (maxLengthAttr == "TwentyHtmlAttribute")
maxLength = commonHtmlAttribute.TwentyHtmlAttribute;
else if (maxLengthAttr == "ThirtyHtmlAttribute")
maxLength = commonHtmlAttribute.ThirtyHtmlAttribute;
else if (maxLengthAttr == "FiftyHtmlAttribute")
maxLength = commonHtmlAttribute.FiftyHtmlAttribute;
else if (maxLengthAttr == "HunderedHtmlAttribute")
maxLength = commonHtmlAttribute.HunderedHtmlAttribute;
else if (maxLengthAttr == "TwoHundredMaxLengthAttribute")
maxLength = commonHtmlAttribute.TwoHundredMaxLengthAttribute;
@if (@data == "Dropdown")
{
var dataList = ddlDropdown[Datasource].OfType<GenericDropDownData>();
placeHolder = (placeHolder == "") ? "Select" : placeHolder;
<div class="col-sm-12 col-md-6 col-lg-6">
<SfDropDownList Enabled="@disable" @bind-Value="@values[filterType.FilterName]" TValue="String" TItem="@GenericDropDownData" Placeholder="@placeHolder" DataSource="@dataList">
<DropDownListFieldSettings Value="ID" Text="Name"></DropDownListFieldSettings>
<DropDownListEvents TValue="String" TItem="@GenericDropDownData" OnValueSelect="(a => OnDropdownChange(a, filterType.FilterName))"></DropDownListEvents>
</SfDropDownList>
</div>
}
@if (@data == "MultiSelectDropdown")
{
var dataList = ddlDropdown[Datasource].OfType<GenericDropDownData>();
placeHolder = (placeHolder == "") ? "Select" : placeHolder;
<div class="col-sm-12 col-md-6 col-lg-6">
<SfMultiSelect TValue="int[]" TItem="@GenericDropDownData" @bind-Value="@selectedFactoryList"
Placeholder="@placeHolder" Mode="@VisualMode.Box" FloatLabelType="FloatLabelType.Always"
DataSource="@dataList" ShowSelectAll="false" EnableSelectionOrder="true"
ShowDropDownIcon="true" FilterBarPlaceholder="@placeHolder" PopupHeight="350px">
<MultiSelectFieldSettings Text="Name" Value="ID"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
}
@if (@data == "TextBox")
{
<div class="col-sm-12 col-md-6 col-lg-6" style="display:@visible;">
<SfTextBox Enabled="@disable" Placeholder="@GetResourceProvider.GetResourceValue(Resource, placeHolder)" FloatLabelType="@FloatLabelType.Always"
Value="@values[filterType.FilterName]" HtmlAttributes="@maxLength"
@onchange="@(a => ValueChanged(a, filterType.FilterName))"></SfTextBox>
</div>
}
@if (@data == "NumericTextBox")
{
<div class="col-sm-12 col-md-6 col-lg-6">
<SfNumericTextBox Placeholder="@GetResourceProvider.GetResourceValue(Resource, placeHolder)" Value="@values[filterType.FilterName]" HtmlAttributes="@maxLength" FloatLabelType="@FloatLabelType.Always"></SfNumericTextBox> @*HtmlAttributes="@commonHtmlAttribute.monthHtmlAttribute"*@
</div>
}
@if (@data == "CheckBox")
{
var isCheckedBox = @values[filterType.FilterName] == "True" ? true : false;
<div class="col-sm-12 col-md-6 col-lg-6">
<SfCheckBox disabled="@disable" @bind-Checked="@isCheckedBox" Value="@values[filterType.FilterName]" @onchange="@(a => ValueChanged(a, filterType.FilterName))"></SfCheckBox>
</div>
}
@if (@data == "RadioButton")
{
var dataDropdown = OnLoadRadioSetAsync(Datasource);
var isRadioChecked = @values[filterType.FilterName] == "True" ? true : false;
<div class="col-sm-12 col-md-6 col-lg-6">
@if (@radioLoad == null)
{
<SfRadioButton Label="test" @bind-Checked="@isRadioChecked"></SfRadioButton>
}
else
{
@foreach (var cnt in @radioGroup)
{
<SfRadioButton Label="@cnt.Name" Name="edition" @onchange="@(a => ValueChanged(a, filterType.FilterName))" @bind-Checked="@isRadioChecked"></SfRadioButton>
}
}
</div>
}
@if (@data == "Date")
{
var dateField = @values[filterType.FilterName] != "" ? DateTime.Parse(@values[filterType.FilterName]) : DateTime.Now;
<div class="col-sm-12 col-md-6 col-lg-6">
<SfDatePicker Enabled="@disable" Value="@dateField" TValue="DateTime?" Placeholder='Choose a Date'
@onchange="@(a => ValueChanged(a, filterType.FilterName))"></SfDatePicker>
</div>
}
}
}
<div class="col-sm-12 col-md-6 col-lg-6">
<SfMultiSelect TValue="int[]" TItem="FactorySetup" ID="MultiSelect" @bind-Value="@selectedFactoryList"
Placeholder="@GetResourceProvider.GetResourceValue(Resource, "lblFactoryList")" Mode="@VisualMode.Box" FloatLabelType="FloatLabelType.Always"
DataSource="@LoadfactoryList" ShowSelectAll="false" EnableSelectionOrder="true"
ShowDropDownIcon="true" FilterBarPlaceholder="@GetResourceProvider.GetResourceValue(Resource, "lblFactoryList")" PopupHeight="350px">
<MultiSelectFieldSettings Text="FactoryName" Value="FactoryID"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
<div class="col-sm-12 col-md-6 col-lg-6">
<SfMultiSelect TValue="int[]" TItem="FactorySetup" AllowFiltering="true" Mode="@VisualMode.Box" @bind-Value="@selectedInActiveList"
Placeholder="@GetResourceProvider.GetResourceValue(Resource, "lblInActiveList")" FloatLabelType="FloatLabelType.Always"
DataSource="@LoadInActivefactoryList" ShowSelectAll="true" EnableSelectionOrder="true" ShowDropDownIcon="true" Enabled="@Enabled"
FilterBarPlaceholder="@GetResourceProvider.GetResourceValue(Resource, "lblInActiveList")" PopupHeight="350px">
<MultiSelectFieldSettings Text="FactoryName" Value="FactoryID"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
</div>
<SfTextBox ID="EmpID" TabIndex="1" @bind-Value="@employee.EmpID" @onkeydown="@keydown"></SfTextBox>
<SfComboBox TabIndex="2" TValue="string" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @onkeypress="@keydown">
<ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
</SfComboBox> |
As per below last reply
Last Reply :-
<EditForm Model="@_testModel" OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText id="name" @bind-Value="_testModel.Name" />
<button>Submit</button>
</EditForm>
@code {
private Test _testModel = new Test();
private void HandleSubmit()
{
}
} |
public void onCreated()
{
JsRuntime.InvokeVoidAsync("onKeyDown", "textbox", null);
} |
function onKeyDown(id) {
document.getElementById(id).addEventListener('keydown', function (args) {
if (args.key === 'Enter') {
args.stopImmediatePropagation();
}
})
} |
sorry still not working (submit behavior validation).please check modified code.
pls check screencast
https://www.screencast.com/t/OoB7iiFajJ
Attachment: BlazorApp1_caa50794.zip
<EditForm Model="@_testModel" OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<SfTextBox ID="textbox" @bind-Value="_testModel.Value1" Created="@(() => onCreated("textbox"))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value1" />
<SfTextBox ID="textbox1" @bind-Value="_testModel.Value2" Created="@(() => onCreated("textbox1"))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value2" />
<SfTextBox ID="textbox2" @bind-Value="_testModel.Value3" Created="@(() => onCreated("textbox2"))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value3" />
<SfTextBox ID="textbox3" @bind-Value="_testModel.Value4" Created="@(() => onCreated("textbox3"))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value4" />
<SfTextBox ID="textbox4" @bind-Value="_testModel.Value5" Created="@(() => onCreated("textbox4"))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value5" />
<button>Submit</button>
</EditForm>
@code {
public void onCreated(string Id)
{
JsRuntime.InvokeVoidAsync("onKeyDown", Id, null);
}
} |
function onKeyDown(id) {
document.getElementById(id).addEventListener('keydown', function (args) {
if (args.key === 'Enter') {
args.preventDefault();
}
})
} |
thanks for reply..
but enter key "tab index" not working which was already worked
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
{
if (args.Key == "Enter")
{
JsRuntime.InvokeVoidAsync("onFocus", "ID");
}
}
IS it possible to get reply ??
because tomorrow support team will not avaialable
<EditForm Model="@_testModel" OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<SfTextBox ID="textbox" @bind-Value="_testModel.Value1" Created="@(() => onCreated("textbox"))" @onkeydown="@((args) => keydown(args,textbox1))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value1" />
<SfTextBox @ref="textbox1" ID="textbox1" @bind-Value="_testModel.Value2" Created="@(() => onCreated("textbox1"))" @onkeydown="@((args) => keydown(args,textbox2))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value2" />
<SfTextBox @ref="textbox2" ID="textbox2" @bind-Value="_testModel.Value3" Created="@(() => onCreated("textbox2"))" @onkeydown="@((args) => keydown(args,textbox3))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value3" />
<SfTextBox @ref="textbox3" ID="textbox3" @bind-Value="_testModel.Value4" Created="@(() => onCreated("textbox3"))" @onkeydown="@((args) => keydown(args, textbox4))"></SfTextBox>
<ValidationMessage For="() => _testModel.Value4" />
<SfTextBox @ref="textbox4" ID="textbox4" @bind-Value="_testModel.Value5"Created="@(() => onCreated("textbox4"))" ></SfTextBox>
<ValidationMessage For="() => _testModel.Value5" />
<button>Submit</button>
</EditForm>
@code {
SfTextBox textbox1;
SfTextBox textbox2;
SfTextBox textbox3;
SfTextBox textbox4;
….
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args, Syncfusion.Blazor.Inputs.SfTextBox reference)
{
if (args.Key == "Enter" && reference != null)
{
reference.FocusAsync();
}
}
} |
Thanks for support
I have below clarification
I have an below field in Editform
1.EmailId
2.PhoneNo
3.Website
How to prevent wrong input in above field??
That means,need to enter correct input value otherwise it should through warning message
[Required]
[Phone]
public string Value1 { get; set; }
[Required]
[EmailAddress]
public string Value2 { get; set; }
[Required]
[Url]
public string Value3 { get; set; } |
Thanks for help.
I have below clarification
1.The above model validation is working fine When I submit editform but I would like to know how to prevent invalid input on email,phone and url before submitting editform that means while entering input.
2.How to auto focus first control in Editform (it may be textbox,combobox, date etc)
<SfTextBox ID="textbox" ValidateOnInput=true Placeholder="Enter Phone number" @bind-Value="_testModel.Value1" Created="@(() => onCreated("textbox"))" @onkeydown="@((args) => keydown(args,textbox1))"></SfTextBox> |
Thanks
above code works fine but FocusAsync() not working for first control
I need to focus first control by default
public async Task UpdateFocus()
{
await textObj.FocusAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
await Task.Delay(500);
await UpdateFocus();
}
} |
I have another below clarification on Enter Key for navigation.
1.As per above code, Enter Key navigation not working for sfdropdownlist
2.In below code,how can I get control id (for keydown event)
<div class="form-group">
<SfComboBox @ref="combobox1" TValue="string" ID="combobox1" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country" @onkeydown="@((args) => Combokeydown(args,combobox2))">
<ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
<ComboBoxEvents TValue=string TItem="Countries"></ComboBoxEvents>
</SfComboBox>
</div>
<div class="form-group">
<SfComboBox TValue="string" @ref="combobox2" ID="combobox2" TItem="Countries" Placeholder="e.g. Australia" DataSource="@Country">
<ComboBoxFieldSettings Text="Name" Value="Code"></ComboBoxFieldSettings>
<ComboBoxEvents TValue=string TItem="Countries"></ComboBoxEvents>
</SfComboBox>
</div>
public void Combokeydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args, Syncfusion.Blazor.DropDowns.SfComboBox<string, Countries> reference)
{
if (args.Key == "Enter" && reference != null)
{
reference.FocusAsync();
//JsRuntime.InvokeVoidAsync("onFocus", "ID");
}
} |
Thanks for reply
For combokeydown,how can I define dynamic TItem.
That means I would like to use this combokeydown event for all combobox
Please help
thanks for support and sorry for late reply..
As per first example code from below link, I have another clarification.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Enter_Focus_164099-1285976318
1."OnValidSubmit" should raise only when I press Enter key on last control (but currently it check for "required validation" when I press enter key on all control because of default submit behavior is "Enter key")
2.I can not get last column value when I press enter key on last control (I need to get all field value from model when I press enter key on last column)
public async Task HandleValidSubmit(EditContext editContext)
{
} |
Thanks for reply...
Noted the above concerns
Can I handle above validation when I click normal button instead of submit button??
Thanks for reply..
Actually my exact requirement is
I need to handle "Tab index" navigation (auto focus to next control) using enter key inside "EditForm".
but when I press "enter key " ,then I am getting validation message from model class
I need to handle validation only on button click not "as Enter Key".
Please advise how to do this.
public void keydown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args, Syncfusion.Blazor.Inputs.SfTextBox reference)
{
if (args.Key == "Enter" && reference != null)
{
reference.FocusAsync(); // focus the next component on enter key action.
}
}
public async Task BtnClick()
{
EC.Validate(); // Validate the component in external button click
} |
function onKeyDown(id) {
document.getElementById(id).addEventListener('keydown', function (args) {
if (args.key === 'Enter') {
args.preventDefault(); // prevent the enter key validation
}
})
} |
As per above comments.I need to get last column value from model or editcontext when I click button during validation
public async Task HandleValidSubmit(EditContext editContext) { } |
My requirment :-
I need to get model value here if validation is success
thanks...
There is final clarification in this discussion.As per continue discussion almost my problem is resolved.
As per below code, focus method is working fine on initial render.I would like to know how to focus on first control when button is clicked.
Note:-
Focus method in button click is working sometimes and not not working sometimes
Please advise what is exact procedure to handle this..
<SfTextBox @ref="TextBoxObj" ID="TextBox1" Value="@Name"></SfTextBox>
<SfTextBox ID="TextBox1" Value="@Name"></SfTextBox>
<SfTextBox ID="TextBox1" Value="@Name"></SfTextBox>
<SfTextBox ID="TextBox1" Value="@Name"></SfTextBox>
<button @onclick="@UpdateFocus">Update Focus to first control</button>
@code {
SfTextBox TextBoxObj;
public string Name { get; set; } = "Hello, World!";
public async Task UpdateFocus()
{
await TextBoxObj.FocusAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
await Task.Delay(500);
await UpdateFocus();
}
}
} |
Please help....
Please check my below details
1.Syncfusion version:19.4
2.Web Assembly
3.Visual Studio 2019 with 5.0 .net framework.
Forgot to attach my code.
please check my code and help how to resolve this issue.
public async Task UpdateFocus()
{
await txt1.FocusAsync();
testing = new TestingClass();
}
Note:-
testing = new TestingClass();
I have call the above code to clear all bind value.
public async Task UpdateFocus()
{
// I have call the below code to clear all bind value.
testing = new TestingClass();
await Task.Delay(100);
await txt1.FocusAsync();
} |