Using Javascript and server side code to handle button click event
-
I am using ASP.Net with C#, framework 3.5. I am working on a page that uses a "Next" button on a master page to inititate a server side click event to do some processing using a session object, and then a Server.Transfer to the next page. The same Master page "Next" button handles all the pages in the app. At some point I am sure that I will need to inroduce a client side click event to do client side validation on one of the pages. What is the best way to do the client side validation and still utilize the server side click event code I already wrote? Or should I abandon my server side click event code and just do everything on the client side?
-
I am using ASP.Net with C#, framework 3.5. I am working on a page that uses a "Next" button on a master page to inititate a server side click event to do some processing using a session object, and then a Server.Transfer to the next page. The same Master page "Next" button handles all the pages in the app. At some point I am sure that I will need to inroduce a client side click event to do client side validation on one of the pages. What is the best way to do the client side validation and still utilize the server side click event code I already wrote? Or should I abandon my server side click event code and just do everything on the client side?
Better if you use ASP.NET validators.Have different validation for different page and have a javascript function,pass it with and the validation group and validate the it using
page_clientvalidate('groupname')
and return false if it fails then your server side event would not fire else return true.It'll continue to postback. For more info about asp.net validators have a look to one of my article Exploring ASP.NET ValidatorsCheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0
-
I am using ASP.Net with C#, framework 3.5. I am working on a page that uses a "Next" button on a master page to inititate a server side click event to do some processing using a session object, and then a Server.Transfer to the next page. The same Master page "Next" button handles all the pages in the app. At some point I am sure that I will need to inroduce a client side click event to do client side validation on one of the pages. What is the best way to do the client side validation and still utilize the server side click event code I already wrote? Or should I abandon my server side click event code and just do everything on the client side?
I agree with Brji... But there may be another way. I try to stay away from client side JavaScript. It's too hard to debug and test. But you could always have the "Next" button postback and make a decision to continue (Response.Redirect(whatever.aspx) or to not continue based on the entered information. I do it quite frequently, but I come from a WinForms background where I'm used to having fine control over the actions of the user. I think anytime you rely on JavaScript, you will get burned in the long run with compatibility issues anyway. Ryan