简体   繁体   English

使用带有参数的ASP.Net中的Javascript调用C#方法

[英]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? 谁能帮助我在带有参数的aspx页面中使用Javascript背后的代码中调用C#方法?

Here is the javascript I have that calls the sample() method: 这是我调用了sample()方法的javascript:

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

I have this c# code to be called that has 2 parameters: 我有两个具有以下参数的c#代码被调用:

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

This gave me an error on the Javascript code: 这给我一个关于Javascript代码的错误:

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. 顺便说一句,当单击图表中的特定条形时,将执行javascript。 I use fusioncharts. 我使用Fusioncharts。

Here's the code on how to call and pass the parameters to the ShowModal javascript function: 以下是有关如何调用参数并将参数传递给ShowModal javascript函数的代码:

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. 扩展David的答案:呈现页面后,有多种方法可以从客户端访问服务器端。 A partial list and in no particular order: 部分列表,无特定顺序:

  • WebMethod
  • UpdatePanel
  • ASMX web service ASMX Web服务
  • WCF web service WCF Web服务
  • 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. 无论如何,您都需要发出AJAX请求(可以使用jQuery非常简单地完成),并处理JavaScript代码中的返回值。 It seems like you are using WebForms . 似乎您正在使用WebForms An UpdatePanel is the WebForms -ish way of doing this. UpdatePanel是使用WebForms方法。 The UpdatePanel takes care of the ajax calls for you, you just need to set it up correctly. UpdatePanel为您处理了ajax调用,您只需要正确设置即可。

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. 只需使用JavaScript即可,因此您不必发回服务器。 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. 请注意,这假设您的HTML中的客户端 id值为lblsample That might not be the case. 事实并非如此。 You'll have to actually examine your HTML and check. 您必须实际检查HTML并进行检查。 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: 您可以嵌入一些服务器端代码,使其在页面加载时运行一次,这将发出该元素的客户端id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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