简体   繁体   中英

Call C# Method using Javascript from ASP.Net with Parameters

Can anyone help me call a C# method in the code behind using Javascript in an aspx page with parameters?

Here is the javascript I have that calls the sample() method:

<script type="text/javascript">
    function showModal(arr, hey) {
        '<%=sample(arr,hey)%>';
     }
</script>

I have this c# code to be called that has 2 parameters:

protected string sample(int i, string a)
{
    lblsample.Text =i + "," + a;
    return lblsample.Text;
}

This gave me an error on the Javascript code:

arr does not exist in the current context

By the way, the javascript will be executed when a particular bar in a chart is clicked. I use fusioncharts.

Here's the code on how to call and pass the parameters to the ShowModal javascript function:

xmlString.AppendFormat("<set label='{3}' value='{1}' tooltext='Employee: {0}{2}Hours:{1}' link='j-showModal-{1}, {1}'/>", dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), "{br}", dt.Rows[i]["EmpNameDot"].ToString()); 

Expanding on David's answer: There are many ways to access the server-side from the client-side after the page is rendered. A partial list and in no particular order:

  • WebMethod
  • UpdatePanel
  • ASMX web service
  • WCF web service
  • Web API
  • SignalR

The one you choose depends on what you are trying to achieve. In any case, you will need to make an AJAX request (this can be done very simply using jQuery ) and handle the returned value in JavaScript code. It seems like you are using WebForms . An UpdatePanel is the WebForms -ish way of doing this. The UpdatePanel takes care of the ajax calls for you, you just need to set it up correctly.

EDIT: If you edit your question with a better explanation of what it is you are really trying to achieve, maybe I can provide a more operative answer.

You can't mix client-side and server-side code like that. The server-side code runs on the server when the page is requested. And at that time it has no knowledge of any client-side events or information.

Why are you trying to do this in server-side code anyway? All you're doing is setting some text on a page element. Just do that with JavaScript so you don't have to post back to the server. Something like this:

function showModal(arr, hey) {
    document.getElementById('lblsample').textContent = arr + ',' + hey;
}

Note that this assumes a client-side id value of lblsample in your HTML. That might not be the case. You'll have to actually examine your HTML and check. There is a workaround where server-side code can help, though. You can embed some server-side code to run once at page load which will emit the client-side id for that element:

function showModal(arr, hey) {
    document.getElementById('<%=lblsample.ClientID%>').textContent = arr + ',' + hey;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM