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

  • Home
  • SEARCH
  • 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 6231275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:54:22+00:00 2026-05-24T09:54:22+00:00

For the first time I am using ajax with asp.net. There for I am

  • 0

For the first time I am using ajax with asp.net. There for I am trying to learn myself some Jquery. I have created an aspx file that contains a button and when this is clicked it’s going to write down the current date. Currently nothing happens when I click the button. I can see the Jscripts are runnig by using firebug but they don’t trigger my alerts. Any ideas?

Code below.

Here’s my Jscript:

$(document).ready(function () {

    $("#Result").click(function () {
        alert("Before Ajax");
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetDate",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                alert('About to replace content');
                $("#Result").text(msg.d);
            }
        });
    });
});

Here’s my aspx file:

<html>
<head>
    <title></title>
    <script src="jquery/JScript1.js" type="text/javascript"></script>
    <script src="jquery/jquery-1.6.2.min.js" type="text/javascript"></script>
</head>
<body>
<form runat="server">
      <input id="Result" type="submit" value="Click here for the time."/>
      </form>
</body>
</html>

and Here’s my aspx codebehind:

public partial class index : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }

Example used: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Here’s the working version:

Jscript:

$(document).ready(function () {

    $("#Result").click(function () {

        $.ajax({
            type: "POST",
            url: "index.aspx/GetDate",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert('About to replace content');
                $("#Result").val(msg.d);
            }
        });
    });
});

aspx:

<html>
<head>
    <title>Calling a page method with jQuery</title>
    <script src="jquery/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="jquery/JScript1.js" type="text/javascript"></script>
</head>
<body>
    <form runat="server">
    <input id="Result" type="button" value="Click here for the time." />

    </form>
</body>
</html>

codebehind:

 public partial class index : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetDate()
        {

            return DateTime.Now.ToString();
        }
    }
  • 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-24T09:54:23+00:00Added an answer on May 24, 2026 at 9:54 am

    The code won’t work because of small errors.

    Fix those and you will be fine.

    1. You are calling the Page Method from public partial class index : Page when it should be ideally from Default: Page
    2. You are using a submit button, so you should use evt.preventDefault();
    3. You are changing the button’s value, so the method to use is .val() and not .text
    4. Lastly, save yourself from some typing with the new Encosia article.

    Sample Code

    $(document).ready(function () {
    
        $("#Result").click(function (evt) {
            evt.preventDefault();
    
            $.ajax({
                type: "POST",
                url: "Test1.aspx/GetDate",
                data: "{}",
                contentType: "application/json",
                success: function (msg) {
                    $("#Result").val(msg.d);
                }
            });
    
        });
    
    });
    

    Happy Coding!

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

Sidebar

Related Questions

I am using ASP.net's AJAX framework and I am trying to do some exception
I'm using Ajax via jQuery for the first time ever. I have an html
I am trying to throw together a website using Ajax for the first time,
I am trying AJAX for the first time on my localhost. I am using
First time using Asp.net-mvc and originally followed the NerdDinner tutorial. My form submit button
I'm using ajax for the first time and have hit a bit of a
I'm trying to decide whether to use a custom ASP.Net Ajax Extender or jQuery
I am using AJAX for the first time with PHP and mySQL. I have
I am using asp.net 3.5 with the ajax toolkit. The problem: I have a
I have been playing with jstree (1.0rc2)+jquery (1.4.2) for the first time with c#.net

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.