Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to call bellow function when the text is changed.

public void text_change(object sender){
}


the below method works fine for JS function and not working when I call the event in the same page as bellow
text.Attributes.Add("onchange", "text_change(text)");


Correct me if I missed anything. Or any other way to achieve this requirement.

Please help me to solve this.Thanks in Advance.

What I have tried:

text.Attributes.Add("onchange", "text_change(text)");
Posted
Updated 14-May-19 20:24pm

1 solution

No, you don't. Trust me on this. If you start handling text changed events in your C# code, then your user is in for a slow and uncomfortable user experience - as every key he types has to do a round trip to the server in order to be processed, and that's slow. Very slow. It also requires a full page load to be started and the whole affected page to be sent back anew to the client.

Handle text changed in your Javascript, not C# - otherwise your UI becomes slow and cumbersome: and your users will go elsewhere!
Remember: Javascript is executed locally on the Client browser - all C# code is executed at the server, so it needs a trip across the internet, fire up the server app to load your page, handle the input, generate new page data, send it back to the client, render it in the browser, and finally the user gets to see what he typed. You can accelerate this to an extent using Ajax, but even then it's not going to be as quick as "press key, handle locally"
 
Share this answer
 
Comments
Member 14369041 15-May-19 2:53am    
Hi OriginalGriff, Thanks for your suggestion. What I need is when user changes any thing on dynamic textbox I want to update it to data base.

I have 20 rows and columns are dynamic on user input.

What I was think to solve this is, first call JS as below:
text.Attributes.Add("onchange", "calculate()");
And call the class library from JS file, please let me how can I achieve this thanks.
OriginalGriff 15-May-19 6:33am    
Why on earth would you want to do that?
Users make mistakes: you have to give them an opportunity to correct it before you commit the data to a DB - or you very quickly end up with a database full of total rubbish which is impossible to sort out. That's why you have Validate events - to check (and process) data when the user had completed an entry.

And "every change" storage in a db is even worse than server processing each character: you need a trip to the server, followed by a round trip to the DB Server, followed by a return to the client. Either that or you DB has to be publicly exposed direct to the internet and that's a recipe for disaster!
Member 14369041 21-May-19 6:59am    
Thanks for the tips, I haves removed that concept.
OriginalGriff 21-May-19 7:02am    
You're welcome!
BillWoodruff 15-May-19 22:52pm    
Amen ! Prometheus stole the Submit button from the Godz and gave it to us for a good reason.

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