16

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.

12
  • Possible duplicate of Here Commented Mar 3, 2015 at 8:33
  • 1
    @Aria.I need the viceverse of what you said. I need to call a method in codebehind from javascript. Commented Mar 3, 2015 at 8:40
  • @charu did you try my answer ? Commented Mar 3, 2015 at 10:03
  • 1
    Please have a look at this one: stackoverflow.com/a/28829291/896276 Commented Mar 3, 2015 at 11:35
  • @Med.Amine.Touil. Yes I tried. But i was not able to add namespace for webmethod. So it is not working. Commented Mar 6, 2015 at 9:39

4 Answers 4

33

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
For the javascript code, I believe you need to use document.getElementById('<%=btnSample.ClientID%>').click();
@Nachiket I want to pass parameters how can i do it?
or add ClientIDMode="Static" to btnSample.
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.

4 Comments

~/code_behind.aspx/Method should be resolved with eg .ResolveClientUrl
~ 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")msdn.microsoft.com/en-us/library/…
You don't need to resolve anything. Just write: url: yourpage.aspx/Method .
Can I do it without static?
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)
                    {

                    }
                });
});

});

2 Comments

try this and let me know if you have still problems
fix a break point in code behind function and check whether it is hitting
-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) {

        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.