You must Sign In to post a response.
  • Category: ASP.NET

    How to call server side function on click of button control from javascript in


    I have a <button type="button" id="Button1">Click</button> control on my page.
    I want to call a function of my .cs file on click of this button. I can't use webmehtod or webservice for that.
    Please help me into this.
    I tried with javascript with following method but not working.

  • #759508
    Hi Anil.

    Try below code.

    < script type="text/javascript">
    function userfunction()
    //your logic
    alert( 'Function is called' );
    </ script >

    < div >
    < asp: Button ID="btnServer" runat ="server"
    Text ="Calling Function" />
    </ div >

    protected void btnServer_Click( object sender,
    EventArgs e)
    ScriptManager .RegisterStartupScript( this, GetType(), "functioname" , "userfunction();" , true);


    Sridhar Thota.
    DNS Member.
    "Hope for the best..Prepare for the worst.."

    Sridhar Thota.
    Editor: DNS Forum.

  • #759513
    Hello Anil Mehta,

    Refer the below code :

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
    <script type='text/javascript'>
    function GetName() {

    PageMethods.Name(Success, Failure);
    function Success(result) {
    function Failure(error) {
    <form id="form1" runat="server">
    <asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
    <asp:Button ID="Button1" runat="server" Text="Get Name" OnClientClick='GetName();return false;' />

    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;

    public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    public static string Name()
    string Name = "Hello Nirav Lalan";
    return Name;

    Hope this will help you.
    Mark the answer if it helped you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759519
    Do you want to use only html button control or you can also use the server side button control.
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #759526
    Yes. I can use only html button control.

    Nirav Lalan: I tried with WebMethod on .cs page but it was giving error of 500.

    anil jain,
    Software Developer,

  • #759557
    Hello Anil Mehta,

    Please can you post your error here. So that we can rectify it.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759614

    for calling on click event in client side, it will refer to the code behind and in code behind we can use java script function which will refer to the design page methods :

    // Design page
    < script type="text/javascript">
    function AddCustomer()
    alert( 'Customer added successfully' );
    </ script >

    <asp:Button ID="btnCustomer" runat="server" Text="Add Customer"
    CssClass="btncontrol" OnClick="btnCustomer_Click" />

    // Code Behind

    protected void btnCustomer_Click(object sender, EventArgs e)
    Page.ClientScript.RegisterStartupScript(typeof(Page), " Message ", "<script>alert('Successfully Logged Out');</script>", false);

    (or) // Directly call javascript method name
    Page.ClientScript.RegisterStartupScript(typeof(Page), " CustomerMessage ", "AddCustomer()", false);

    Hope its helpful for uuu..


  • #759621
    Hi Nirav,

    I tried the same code as you given and
    i got the below error in inspect element in chrome:

    POST http://localhost:36072/test/Name 405 (Method Not Allowed)

    The WebMethod which we create in .cs file is not getting called.

    Please help me into this


    anil jain,
    Software Developer,

  • Sign In to post your comments