0

I am using ASP.NET

If I want to execute my Insert command from my SqlDataSource in my code behind I would do this.

SqlDataSource1.Insert()

But how would I be able to do this in JavaScript?

<script type='text/javascript' language="javascript"> 

  function valSubmit() {

  //Need to call the Insert Command here!

  }

</script>

OR

Where in my code behind can I place the Insert() command to make sure only after my valSubmit() function in JavaScript have executed it will then execute the Insert Command?

4
  • 1
    Do you mean to run the SqlDataSource1.Insert() from JavaScript side ? As I know there is no way for direct call any function of some programming language through the JavaScript. The only solution, If I am correct is AJAX call to some URL of your web application that corresponds to a function in your application and in turn that function call your SqlDataSource1.Insert(). I hope that is what you mean Commented Sep 27, 2011 at 8:30
  • Yes that is what I want to do....but do I have to use AJAX? it sounds way to complex as I am sure there must be a easy way to do this without using AJAX. Commented Sep 27, 2011 at 8:32
  • 1
    So you have to create a functino/method in your application that corresponds to an URL. In example you can use a url like mysite.ext/submitvalue.asp(x) and then in your application to create a method/function in appropriate location that handled by that url. From within that function/method you can now call your SqlDataSource1.Insert() and do other operations as well ! Commented Sep 27, 2011 at 8:36
  • 1
    Javascript runs on the client side. You have to transfer the data to your server side (using regular http post or AJAX) and execute the insert command on the server. You definitely need to learn the different between client side and server side. Commented Sep 27, 2011 at 8:40

2 Answers 2

0

Create a Web Handler that will invoke the Insert method and call this handler using your preferred AJAX method.

1
  • Sounds to complex, there must be a easy way to execute the command with JavaScript..... Commented Sep 27, 2011 at 8:33
0

Create Ajax method Example of Ajax method

$.ajax({ type: POST, url: url, data: data, success: success, dataType: dataType }); function success() { alert(data saved); }
URL : a new page for example ajax.aspx.On this page load you can write your method for insertion. Data : Parameters, These will be accessed using Request.Querystring

and call server methods which are used for insertion or any other operation.

1
  • You forgot to mention that this is jQuery, which the OP may not actually be using in their project. Commented Sep 28, 2011 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.