0

i wonder if there is any way to call an asp method that returns a string or something when you are in <script></script> tags.

Like this (it does not work):

    <script language="javascript" type="text/javascript">
    function isValid() {
        var required = "<%= callAspMethodThatReturnsSomething() %>";
    }
    </script>

any help with this? thx!

4
  • This should work if the method is defined in the code behind. Can you explain what exactly doesn't work? Commented May 5, 2011 at 9:01
  • When i run this code, then requiredgives me null. Actually the method is not in codebehind. I am working with masterpages and the method is in the masterpage. So i do Master.Method() in codebehind to get the method... Commented May 5, 2011 at 9:03
  • How does the final rendered HTML look like when you browse the source code? Commented May 5, 2011 at 9:04
  • it gives me nothing, the ASP code disappears in javascript like this: var required = ""; Commented May 5, 2011 at 9:20

3 Answers 3

2

There is no direct way to call c# method like in example above. But you can send $.ajax request to the server, or even send postback using _doPostBack(..,..)

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

4 Comments

The code is within a server side code block <%%>, so it runs server side. Not sure what you are talking about.
@Oded: True, but seems @Ozkan want to execute c# method from client side and not just during page rendering.
not if he put the server side code in server side code blocks. I think he wants the return value (presumably a string) to be output to the javascript.
@Oded: Yeah, i see, seems your true. I just misunderstand question for first time. Thanks for pointing it out. +1 ;)
1

The issue is that you are calling a method on the master page from the content page directly.

You can't do that - you need a reference to the strongly typed master page.

Try this (asp.net 4.0):

<%:((MasterPageClassName)Page.Master).callAspMethodThatReturnsSomething()%>

Or this (pre 4.0):

<%=((MasterPageClassName)Page.Master).callAspMethodThatReturnsSomething()%>

Comments

0

<% callAspMethodThatReturnsSomething() %>

this method in <% %> will work before what you want to do ,it may looks like does not work

yet,you can create a new button on page ,make it unvisible and use it's click event like document.getElementById("<%=button1.ClientID%>").click()

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.