Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4030210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:26:33+00:00 2026-05-20T11:26:33+00:00

In GridViewData.aspx.cs I have a method I want to call inside a javacsript function.

  • 0

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")           
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:26:34+00:00Added an answer on May 20, 2026 at 11:26 am

    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:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        <!--
            $(function () {
                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/ValidateNameUpdateable",
                    data: "{\"input\":5}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        // (msg.d is the retrieved data)
                        var d = msg.d;
                        if (d == 22) {
                            alert("OK");
                        }
                        else {
                            alert("Not OK");
                        }
                    },
                    error: function (msg) {
                    }
                });
            });
        //-->
        </script>
    </body>
    </html>
    

    WebService.cs (in App_Code):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [System.Web.Script.Services.ScriptService]
    [WebService(Namespace = "http://tempuri.org/")] // <-- Put something like: services.yourdomain.com in here.
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService : System.Web.Services.WebService {
    
        [WebMethod]
        public int ValidateNameUpdateable(int input)
        {
            return input == 5 ? 22 : -1;
        }
    }
    

    WebService.asmx:

    <%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
    

    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:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        <!--
            $(function () {
                function getRecord(id) {
                    $.ajax({
                        type: "POST",
                        url: "WebService.asmx/GetRecord",
                        data: "{\"id\":" + id + "}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            // (msg.d is the retrieved data)
                            var d = $.parseJSON(msg.d);
                            if (d.RecordExists) {
                                alert("Record with id: " + d.ID + "\nFirstName: " + d.FirstName + "\nLastName: " + d.LastName);
                            }
                            else {
                                alert("Record doesn't exist.");
                            }
                        },
                        error: function (msg) {
                        }
                    });
                }
    
                getRecord(1);
                getRecord(4);
                getRecord(0);
            });
        //-->
        </script>
    </body>
    </html>
    

    WebService.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Script.Serialization;
    
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [System.Web.Script.Services.ScriptService]
    [WebService(Namespace = "http://tempuri.org/")] // <-- Put something like: services.yourdomain.com in here.
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService : System.Web.Services.WebService {
    
        [Serializable]
        protected class Record
        {
            public bool RecordExists { get; set; }
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public Record() // Initializes default values
            {
                RecordExists = true;
                ID = 0;
                FirstName = "";
                LastName = "";
            }
        }
    
        [WebMethod]
        public string GetRecord(int id)
        {
            // Initialize the result
            Record resultRecord = new Record();
            resultRecord.RecordExists = true;
            resultRecord.ID = id;
    
            // Query database to get record...
            switch (id)
            {
                case 0:
                    resultRecord.FirstName = "John";
                    resultRecord.LastName = "Something";
                    break;
                case 1:
                    resultRecord.FirstName = "Foo";
                    resultRecord.LastName = "Foo2";
                    break;
                default:
                    resultRecord.RecordExists = false;
                    break;
            }
    
            // Serialize the result here, and return it to JavaScript.
            // The JavaScriptSerializer serializes to JSON.
            return new JavaScriptSerializer().Serialize(resultRecord);
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Aspx Page: $(document).ready(function() { $(#btnn).click(function() { $.ajax({ type: POST, url: TestPage.aspx/emp, data: {}, contentType:
This is really bugging me. I'm using a GridView and want to format it
I have a gridview data and this gridview have subgrid too. in the first
I have to filter GridView data based on the Tab Panel selected, Here is
I have a gridview with data. I need to put one button to Export/import
I have a masterpage with a sidebar that conatians an accordion control for site
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ds
Currently I am facing the tedious problem of exporting complete GridView data to the
Hi I was just wondering what is the best way to fetch gridview data
How to create and download excel document using asp.net ? The purpose is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.