In GridViewData.aspx.cs I have a method I want to call inside a javacsript function.
//This simple example method is from GridViewData.aspx.cs
private int ValidateNameUpdateable()
{
return 22;
}
I want to call this function from Javascript in default.aspx
<script type="text/javascript">
function validateForm()
{
if (ValidateNameUpdateable==22)
{
alert("Name is not updateable, the party already started.");
return false;
}
//if all is good let it update
UpdateInsertData()
}
</script>
Now hears the kicker, I am mostly using jqUery as UpdateInsertData() is a jquery Call and works fine. How do I use ValidateNameUpdateable to call jQuery to return the value from the c# method. . I believe this issue is with my jQuery Call as its just posting and I need to do a $.get or something?
function ValidateNameUpdateable()
{
$(document).ready(function ()
{
$.post("GridViewData.aspx")
});
}
Take a look at this question:
Using jQuery's getJSON method with an ASP.NET Web Form
It shows how to do it.
An example:
Default.aspx:
WebService.cs (in App_Code):
WebService.asmx:
I hope this explains the idea.
If you want to be able to pass more advanced structures (no int, or string) you might want to consider using JSON.
For the parsing of JSON I’ll use the JQuery function parseJSON
All you need to do is create a structure on the web service (struct), and serialize it using the JSON serializer.
Another example using JSON:
Default.aspx:
WebService.cs:
Please note that AJAX is Asynchronous, that means that even tough the pages are requested in a specific order, they are not received in a specific order. That means that even tough you request the records in order: 1, 4, 0, they can be received in any order, like 4, 1, 0, or 1, 0, 4.