Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Ajax jQuery
i just want to simply make a webmethod call from a asp button. When i use a html button it works perfectly but the asp button does not work.
 
my Default.aspx page is
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
 
 <html>
 
  <head>
      <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
      <script src="Scripts/myjscript.js" type="text/javascript"></script>
 
  </head>
  <body>
      <input type="submit" value="Submit" id="btnSubmit"/><br/>
      <form id="form1" runat="server">
          <asp:Button runat="server" ID="btnasp" Text="ASP Button"/>
 
      </form>
  </body>
 
 </html>
 

and Default.aspx.cs code is
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        [WebMethod]
        public static string getDate()
        {
            return DateTime.Now.ToString();
        }
    }
}
 
Jscript file is
 
$(function () {
 
    $("#btnSubmit").bind("click", onClick);
    $("#btnasp").bind("click", onASPClick);
 
});
 
function onClick() {
 
    alert("I am a HTML Submit Button");
    callMethod();
}
 

 
function onASPClick() {
 
    alert("I am a Asp.net Button");
    callMethod();
 
}
 

function callMethod() {
    $.ajax({
        url: 'Default.aspx/getDate',
        type: "POST",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(data.d);
        }
    });
 
}
 
somebody please help.
Posted 23-Dec-12 22:26pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If it were me I wouldn't bind the function to the button like this, but use the 'OnClientClick' attribute directly on the button itself.
 
Try:
 
<asp:Button runat="server" ID="btnasp" OnClientClick="onASPClick(); return false;" Text="ASP Button"/>
 

It's important to specify the 'return false' because you want it to perform the click but not submit the form.
  Permalink  
Comments
Ehtesam Ahmed at 24-Dec-12 4:06am
   
Thanx Nick
That solved my problem but now i have another problem that the click event fire twice. Can you please help?
Nick Fisher (Consultant) at 24-Dec-12 4:09am
   
Hmmm. Did you make sure you removed the old line:
 
$("#btnasp").bind("click", onASPClick);
 
Now that you've added the 'OnClientClick' command, it may be doing funny things if you're still trying to bind to it the other way.
Ehtesam Ahmed at 24-Dec-12 4:33am
   
i found the solution i need to add this UseSubmitBehavior="False" :)
Nick Fisher (Consultant) at 24-Dec-12 4:37am
   
Ah OK, cool. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 210
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 mukesh mourya 85
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100