简体   繁体   English

我如何从javascript调用C#函数?

[英]How I would call a C# function from javascript?

I'm trying to figure out how I would call a C# function from a javascript confirm box. 我试图弄清楚如何从javascript确认框中调用C#函数。 ie If user selects 'OK' a function is called, and if user selects 'Cancel' another function is called. 即,如果用户选择“确定”,则调用一个功能,如果用户选择“取消”,则调用另一个功能。 All the code is located in the back page, and looks as follows: 所有代码都位于背面,如下所示:

Response.Write(@"
    <script language='javascript'>
    var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.');
    if (msg==true) {<%=redirect()%>;}
    else {<%=clearForm()%>;}
    </script>
");

protected void redirect(object sender, EventArgs e)
{
    Response.Redirect("myPage.aspx");
}

protected void clearForm(object sender, EventArgs e)
{
    //More code here//
}

Note that all the code within the Response.Redirect is all on one line, I just split it up here for simplicity! 请注意,Response.Redirect中的所有代码都在一行上,为简单起见,我在这里将其拆分! Anyways, this does not work, and I cant find a solution. 无论如何,这是行不通的,我找不到解决方案。 I've tried various different things within the if statement 我在if语句中尝试了各种不同的方法

My first idea was not to give the user an option, and to simply use: 我的第一个想法不是给用户一个选项,而是简单地使用:

Response.Write(@"<script language='javascript'>alert('Your new document has been created.');</script>");
Response.Redirect("TaskPanel.aspx");

But when I tried this, the page did not wait for the user to click OK before redirecting, and hence made it pointless. 但是,当我尝试此操作时,页面没有等待用户单击“确定”再进行重定向,因此变得毫无意义。

You are getting confused between server-side and client-side code. 您在服务器端代码和客户端代码之间感到困惑。 The C# code you posed will run on your server and write out the JavaScript code on the page: 您提出的C#代码将在您的服务器上运行,并在页面上写出JavaScript代码:

<script language='javascript'>
    var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.');
    if (msg==true) {<%=redirect()%>;}
    else {<%=clearForm()%>;}
</script>

The JavaScript you write out will be run on the client-side browswer. 您写出的JavaScript将在客户端浏览器上运行。

However, I would imagine you will get a JavaScript error, since it will attempt to write out <%=redirect()%>; 但是,我想您会遇到JavaScript错误,因为它将尝试写出<%=redirect()%>; and <%=clearForm()%>; <%=clearForm()%>; which is ASPX scriptlet code that should have been run on the server-side (but is actually appearing as a String in the JavaScript on the client). 这是应该在服务器端运行的ASPX scriptlet代码(但实际上在客户端的JavaScript中显示为字符串)。

This is just an explanation/answer to why you cannot call a C# function directly from JavaScript. 这只是为什么不能直接从JavaScript调用C#函数的解释/答案。

For your specific problem, you do not need to call back to the server to do a redirect or clear form. 对于您的特定问题,您无需调用服务器即可进行重定向或清除表单。

Both of these can be done in JavaScript. 这两个都可以用JavaScript完成。

The redirect can be done with JavaScript window.location="myPage.aspx" or self.location="myPage.aspx" 可以使用JavaScript window.location="myPage.aspx"self.location="myPage.aspx"完成重定向

The clear form can be done with JavaScript form.reset() 可以使用JavaScript form.reset()来完成清除表单

Yes its simple just : put you callback function in a file 是的,它很简单:将回调函数放在文件中

After that you should bind click on OK you call the function with : AJAX with any javascript Framework to make simple 之后,您应该单击“ 确定”进行绑定,然后使用以下命令调用该函数: AJAX和任何javascript框架,以使其变得简单

OK Cancel 确定取消

$("#OK,#Cancel").click(function(){

 var whichFct = $(this).attr('fct');      var urlToFile = "web/app/link/fct/1";
 if(whichFct == 2)  urlToFile = "web/app/link/fct/2"

   $.post(urlToFile,function() {
            //do sthlike hiding the form ...
       });
});

Its just simple than this 它比这简单

pass a javascript function on buttonclick.. then write following code in .aspx page: 在buttonclick上传递一个javascript函数。然后在.aspx页中编写以下代码:

<script language='javascript'>
function func_name()
{
var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.')
if (msg==true) {<%=redirect()%>}
else {<%=clearForm()%>}
}
</script>

do similarly for other button as well. 其他按钮也是如此。

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

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