15

I want to call a function from my code behind using javascript. I used the below code:

function fnCheckSelection() {
some script;
window["My"]["Namespace"]["GetPart"](null);
}

...where "GetPart" is the function name. However, this is not working. Please help me on this.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
charu
  • 389
  • 2
  • 4
  • 12

4 Answers4

31

in JavaScript:

    document.getElementById("btnSample").click();

Server side control:

    <asp:Button runat="server" ID="btnSample" ClientIDMode="Static" Text="" style="display:none;" OnClick="btnSample_Click" />

C#

    protected void btnSample_Click(object sender, EventArgs e)
    {

    }

It is easy way though...

Joel Christophel
  • 2,604
  • 4
  • 30
  • 49
Nachiket
  • 620
  • 6
  • 23
  • It will reload as obvious. When you click the server control, ASP.NET always loads the page and then call the event which is triggered from JavaScript call. Its like we are clicking the button without mouse. – Nachiket Mar 03 '15 at 10:43
  • 2
    Yeah. But if i use Update Panel, I can update the particular portion right? And my grid which is supposed to update is inside the update panel. But still it is not working. Do you have any idea on this. – charu Mar 06 '15 at 09:41
  • 3
    For the javascript code, I believe you need to use document.getElementById('<%=btnSample.ClientID%>').click(); – dcp Jan 19 '18 at 15:43
  • @Nachiket I want to pass parameters how can i do it? – IdkWhy Jul 27 '18 at 15:43
  • 1
    or add `ClientIDMode="Static"` to `btnSample`. – KZee Mar 21 '20 at 18:07
10

You can do this by an ajax call

this is a jquery example:

$.ajax({
            type: "POST",
     url:"~/code_behind.aspx/Method",
            data: dataPost,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
    ....
    });

here is api documentation and in code behind

[WebMethod]
public static yourType Method (Params){}

or you can add a hidden button inside updatePanel, and invoke the click event using js. ('#<%=ID.ClientID%>').click(); It will invoke the OnClientClick if it exists then your codeBehind fucntion.

Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15
  • `~/code_behind.aspx/Method` should be resolved with eg `.ResolveClientUrl` –  Mar 03 '15 at 08:51
  • `~` is no valid qualifier in pure HTML, it does only work for ASP.net and needs resolving according to the applications virtual path, which can be achieved with eg [`.ResolveClientUrl("~/foo.aspx")`](https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx)https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx –  Mar 03 '15 at 08:56
  • You don't need to resolve anything. Just write: `url: yourpage.aspx/Method` . – Roland Jan 13 '17 at 12:15
  • 1
    Can I do it without static? – Cibo FATA8 Nov 28 '19 at 19:30
5

try this

Your code behind function

[WebMethod]
public  static void GetPart() {

               //your code goes here
}  

. Javascript

$(document).ready(function () {

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

 $.ajax({
                    type: "POST",
                    url: "/youraspxpagename.aspx/GetPart",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg)
                    {

                    }
                });
});

});
Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
-1

Using ajax, you can call a codebehind function from javascript using JQuery:

function : fnCheckSelection(){
    $.ajax({
        cache: false,
        url: "/GetPart"
        type: "POST",
        success: function (result) {

        },
        error: function (msg) {

        }
    });
}
Inspector Squirrel
  • 2,548
  • 2
  • 27
  • 38