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 asp.net

    Hi,

    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.

    document.getElementById("Button1").click();
  • #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"
    OnClick="btnServer_Click"
    Text ="Calling Function" />
    </ div >

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


    Regards.

    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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script type='text/javascript'>
    function GetName() {

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



    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)
    {
    }

    [WebMethod]
    public static string Name()
    {
    string Name = "Hello Nirav Lalan";
    return Name;
    }
    }

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

    Regards,
    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 asp.net 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,
    NEITL,Mumbai.

  • #759557
    Hello Anil Mehta,

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

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

  • #759614
    Hi,

    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..

    Thanks,
    Chitaranjan

  • #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

    Thanks

    anil jain,
    Software Developer,
    NEITL,Mumbai.


  • Sign In to post your comments