0

I am a beginner in Javascript.

I have a javascript function

Print: function () {
......
}

in my "testing.js" file.

now I need to call this "Print" function from a code behind method. How to do...?

I tried all the below but failed every time

http://forums.asp.net/t/1785410.aspx?how+to+call+javascript+function+from+code+behind+cs+file+

How to call this javascript function from code behind

and many many..... but didnot get a solution..Any help me to come out from this?

I was working for a day in this and all in vain.

5
  • Is the 'mvc' tag referring to ASP.NET MVC? If it is, then there's no real notion of 'code behind' in ASP.NET MVC... Commented Mar 17, 2014 at 12:08
  • @PatrykĆwiek: HI, yes ASP.NET MVC, I would like to have a method name print and in that method i would like to call this JS "Print" function Commented Mar 17, 2014 at 12:13
  • You don't call javascript functions directly from server code, it's completely separate domains. The javascript page doesn't even exist yet unless the server code is invoked from Ajax. Commented Mar 17, 2014 at 12:16
  • 1
    It may be helpful to get a better picture of what you are trying to accomplish. When you say "code behind", are you wanting the server to tell the browser to print? Or are you wanting a button on your page to call the print function? Or something else? Commented Mar 17, 2014 at 12:27
  • 1
    your question suffers from the XY problem. You should ask how to solve your problem, rather than ask why your solution doesn't work. meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Mar 17, 2014 at 12:33

1 Answer 1

1

asp.net mvc and asp.net win-forms are different technologies. in mvc you do not have a code behind, there is a better isolation between server and client side. there for there are some options depend on you'r status :

1)if the trigger happens on client you can bind this trigger to a js or jquery function

jquery Example:

$('#IDOFButton').bind('click',function(){
   //call print function
   print();
});

2)if the trigger happens on server you can send back a js in a script tag to run immediately as it returns to client

public JavaScriptResult Print()
        {
            JavaScriptResult js = new JavaScriptResult();
            js.Script = "alert('hello');";
            return js;
        }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.