1

hello i have this question i was trying to find a way to call a javascript function from asp controllers and i did here is the code :

    <script type="text/javascript">
      function hello() { 
        alert("hello world")
    }
</script>
<  /head>
 <body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="buttonme" runat="server" OnClientClick="javascript:hello()" 
 Text="click" />

now lets say i have this code behind function

    Public Sub msg()
      MsgBox("hello world")
    End Sub 

and i want to call it from a javascript function so it will be like controller----call---> javascript ---call--->code behind

is there is a way to do this

3
  • since this is a web app, when the MsgBox is fired, it will only popup a box on your server, it will not popup a box on the client. You will need to use ajax in order to use code-behind files for functions. Commented Jun 14, 2012 at 19:03
  • @Juventus18 thanks for your reply this is just an example it can do any thing Commented Jun 14, 2012 at 19:08
  • 1
    Take a look at his answer: stackoverflow.com/q/10893954/1268570 this is an alternative using Rest services (without .svc files) Commented Jun 14, 2012 at 19:14

2 Answers 2

4

Define your method in a class somewhere in your web application.

[System.Web.Services.WebMethod]
public static string Msg()
{
     return "Hello world";
}

Add a script manager with EnablePageMethods=true in your aspx page

<asp:ScriptManager ID="someId" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

Call the method in javascript

<script language="javascript" type="text/javascript">
    PageMethods.Msg(onSuccess);
    function onSuccess(response)
    {
        alert(response);
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You must use AJAX in some form and expose the ASP.NET method you want to call via HTTP.

8 Comments

is there is any other way to do this with out ajax
Not really, no. If you want the browser-side code to call server-side code the communication mechanism is via AJAX (the XMLHttpRequest object). What's wrong with using AJAX? jQuery makes it quite easy to do.
i know it isn't hard , what i have is a linkbutton that calls a javascript function to open a new tab in my project but before opening this tab i need to update a session , so my problem know is to update this session before opening the new tab
Update the ASP.NET Session? Any HTTP request back to the server from the browser will do that. Is the content in your pop-up served from your application?
considering this OnClientClick="var url = 'CIP_frmCIPEventExplorePdf.aspx?ViewPDF=1&intCIPEventID=' + $('#intCIPEventID').val();parent.showNewTab(url, 'CIP Chart - PDF', 'CIP');"> i want this control to go to a code behind function update the session "" and then "" open the new page , this new page generat a pdf file with the old session if you reload the page you get the updated data
|

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.