Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox in blazor

<input type="text" id="lblname" @bind="@value" />


At runtime I assigned textbox value as 1 through javascript.

document.getElementById("lblname").value = 1


It is displaying in a page.

However, I want to save the assigned textbox value to a variable. When I assign

id=@value

It always shows null value.
Iam not entering in textbox at runtime. Instead value is assigned. How to get the changed textbox value

What I have tried:

I have a textbox in blazor

<pre><input type="text" id="lblname" @bind="@value" />


At runtime I assigned textbox value as 1 through javascript.

document.getElementById("lblname").value = 1


It is displaying in a page.

However, I want to save the assigned textbox value to a variable. When I assign

id=@value

It always shows null value.
Iam not entering in textbox at runtime. Instead value is assigned. How to get the changed textbox value
Posted
Updated 14-Sep-21 21:57pm

1 solution

Apparently, setting the value directly via Javascript doesn't trigger the necessary events for Blazor to notice the update.

This StackOverflow thread has a couple of suggestions for you to try:
c# - Changing an Input value in Blazor by javascript doesn't change it's binded property value - Stack Overflow[^]

Either trigger the change event after setting the value:
JavaScript
const lbl = document.getElementById("lblname");
lbl.value = 1;
lbl.dispatchEvent(new Event("change"));
or invoke a C# method to update your property:
JavaScript
DotNet.invokeMethodAsync('UpdateValue', '1');
C#
public void UpdateValue(string newValue)
{
    value = newValue;
}
 
Share this answer
 
Comments
Member 13964245 16-Sep-21 1:31am    
Thanks for your answer. As u suggested I solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900