0

First off, I know there are so many questions that are similar to this, but none of them have helped in my particular case (it's likely that I just need clarification, and I don't want to open an old thread).

I am creating an application and I need to call a C# method within a jQuery function. The reason I need to do this, is because I need to perform a query to a database using two specific parameters.

Javascript:

$(".button").click(function(){
    var dataId = $(this).attr("data-Id");

    var s = PageMethods.returnStr(dataId, onSuccess, onError);
    function onSuccess(result){
        alert(s);
    }
    function onError(result){
        alert("error");
    }
});

C#:

[WebMethod]
public static string returnStr(string Id)
{       
    // ... blah blah make query, return string s
    return s;
}

The returned string is undefined. If I use alert(result), I just get the syntax of the page.


I've also tried to reference a c# variable using:

C#

public static string s;

JavaScript:

alert('<%=s%>');

and this works, but then when I call the WebMethod, I cannot modify s.


One last thing I've tried is ajax, but even if I model it off of answers found on SO, I cannot get it to work as I'd like it to - it returns "[object Object]":

AJAX:

$.ajax({
    type: "POST",
    url: "default.aspx/returnStr",
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    error: function(result){
        alert("request failed" + result)
    },
    success: function(result){
        alert(result);
    }
});

Any advice at all is greatly appreciated!!

7
  • 2
    How are you calling the C# method? JavaScript is sandboxed, so it shouldn't be possible. Commented Nov 1, 2016 at 17:41
  • I'm not sure I 100% understand what you mean. I'm calling the function like tihs: var s = PageMethods.populateInstructions(dataToggle, dataId, onSuccess, onError); Commented Nov 1, 2016 at 17:43
  • 2
    Show the code that defines PageMethods. Commented Nov 1, 2016 at 17:44
  • <asp:ScriptManager runat="server" EnablePageMethods="true" EnablePartialRendering="true"> I was under the impression that adding 'EnablePageMethods="true"' allowed me to use 'PageMethods.{C#functionName}'. Am I missing something big here? Commented Nov 1, 2016 at 17:49
  • Your AJAX is working, the problem is because you're using alert() to debug. Use console.log(result) instead Commented Nov 1, 2016 at 17:51

1 Answer 1

2

You cannot do what you are proposing. Javascript is run in the web browser. C# is run on the server. You cannot run C# code in the browser.

Using alert('<%=s%>'); works because it is evaluated at the server before being sent to the client.

You have two options:

  1. Make a pure-Javascript function that does what you need.
  2. Use the normal web forms event handlers from an ASP control (i.e. the OnClick method for your button).
  3. Make an AJAX call back to the server to do what you need.

I see you made an attempt at #3, but you did not do it correctly. The URL at "default.aspx/returnStr" doesn't mean anything. It will not automatically run the returnStr method inside default.aspx.cs.

You're better off using #2. It'll be the easiest.

Update: Another option is to use an asp Button, like #2, then in the OnClick method, use ClientScript.RegisterStartupScript, which will inject Javascript into the page after the postback completes. In that javascript you inject, you can run the code to open the popup.

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

7 Comments

The problem is that I'm creating a dynamic button which when clicked utilizes a pop-up functionality (vast-engineering.github.com/jquery-popup-overlay). Based on the way that functions, I can't use a code-behind button click event. Either I need to invoke both a C# button click method and the jquery method that triggers the popup functionality, or I need to reference the C# from within the jquery function. Based on what you're saying, AJAX may be my best bet (?)
What is the returnStr method doing that it must be run on the server?
I need to make a query to a database in order to pull a result that is matched to each individual button. For example, I make buttons 1 through 5. I then have rows 1 through 5 in my table. I need to query row 1 for button 1, row 2 for button 2 etc.
Is it really just 5 values? You could pull all 5 values from the database on page load, and embed those into the javascript so they are already available when the buttons are clicked.
no, in reality it's > 150 or so. But still if I pull all the values on page load, I'd still have to be able to access the data within the JS. I suppose I could have a C# array, and reference it like <%= csArray[x] %>
|

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.