Javascript calling a c# method
-
Hi everyone, I have a form on my website and I'm thinking about creating a little javascript function to perform validation. I know asp.net has validation built in, but there are some other things I'd like the function to do that can't be done with the asp.net validation plus it's a learning project. I have a button at the end of the form which I would like to call this javascript function which will validate the form input fields, but then after that I'd like it to call the c# code behind which will access the database and do some other server side functions. Is there a way this can be done? Can you attach two "onClick" event handlers to a single button, or do I have to do something with a "hidden button" that is linked to the codebehind and have the javascript "click" it when it's done? Thanks!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
Hi everyone, I have a form on my website and I'm thinking about creating a little javascript function to perform validation. I know asp.net has validation built in, but there are some other things I'd like the function to do that can't be done with the asp.net validation plus it's a learning project. I have a button at the end of the form which I would like to call this javascript function which will validate the form input fields, but then after that I'd like it to call the c# code behind which will access the database and do some other server side functions. Is there a way this can be done? Can you attach two "onClick" event handlers to a single button, or do I have to do something with a "hidden button" that is linked to the codebehind and have the javascript "click" it when it's done? Thanks!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
Hi everyone, I have a form on my website and I'm thinking about creating a little javascript function to perform validation. I know asp.net has validation built in, but there are some other things I'd like the function to do that can't be done with the asp.net validation plus it's a learning project. I have a button at the end of the form which I would like to call this javascript function which will validate the form input fields, but then after that I'd like it to call the c# code behind which will access the database and do some other server side functions. Is there a way this can be done? Can you attach two "onClick" event handlers to a single button, or do I have to do something with a "hidden button" that is linked to the codebehind and have the javascript "click" it when it's done? Thanks!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
First, you must understand that when you write javascript code it runs on client side and your c# code runs on server side. So you can have both, client validation written in javascript by attaching code to the onclick event and server validation written in C#. There's no ambiguity if that's what worries you. Hope this helps.
Kenia
-
Ah, that's the ticket! I had a general idea of what I needed the browser to do, but just wasn't sure what it was called. Then I tried googling for "javascript to click a hidden button" or "attaching multiple events to a button" but wasn't getting anywhere. Thank's again for the help!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
First, you must understand that when you write javascript code it runs on client side and your c# code runs on server side. So you can have both, client validation written in javascript by attaching code to the onclick event and server validation written in C#. There's no ambiguity if that's what worries you. Hope this helps.
Kenia
Hi Kenia, Thanks for taking the time to respond :) I've done a lot of server side coding but there are some things that I would like to have the browser do without issuing a post back (server side). I was searching on google for a couple of hours before posting my question and I think by that point I was just so confused myself that I didn't word it too well. Turns out I was really just trying to figure out if it was possible to have the javascript do a post back so the server can perform what it needs to do. This way, I can have my submit button run the function on the client, and if there's a problem it can pop up a window right away before ever having to send any info back to the server :)
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
I also found this site very helpful in understanding the _doPostBack javascript function. aspalliance
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.