javascript 如何在asp:button 上调用javascript 函数和c# 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17596999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:55:16  来源:igfitidea点击:

How to call javascript function and c# code on asp:button?

c#javascriptasp.net

提问by Yulia V

When a user clicks a button on ASP.net page, I need to

当用户单击 ASP.net 页面上的按钮时,我需要

  1. Save file from asp:fileUploadin a folder on a server - I guess this needs to be done in C#, like in How to correctly use the ASP.NET FileUpload control

  2. Run a javascript function like in How to call javascript function from asp.net button click event

  1. asp:fileUpload服务器上的文件夹中保存文件- 我想这需要在 C# 中完成,例如如何正确使用 ASP.NET FileUpload 控件

  2. 运行 javascript 函数,如如何从 asp.net 按钮单击事件调用 javascript 函数

Is there a way to combine C# and Javascript to achieve what I need? If not, how should I do it?

有没有办法结合 C# 和 Javascript 来实现我所需要的?如果没有,我该怎么做?

回答by drigoangelo

Try using the onClientClickproperty of the asp:buttonelement.

尝试使用元素的onClientClick属性asp:button

Ex, on your .aspx file:

例如,在您的 .aspx 文件中:

<script type="text/javascript">
  function myFunction()
  {
    alert('hi');
  }    
</script>
...

<asp:button id="Button1"
   usesubmitbehavior="true"
   text="Open Web site"
   onclientclick="myFunction()"
   runat="server" onclick="Button1_Click" />

And in your code behind (.aspx.cs)

在你的代码后面(.aspx.cs)

  void Button1_Click (object sender, EventArgs e)
  {
    if (this.FileUpload1.HasFile)
    {
        this.FileUpload1.SaveAs("c:\" + this.FileUpload1.FileName);
    }

  }

More info at

更多信息在

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

回答by David

Note that no JavaScript actually "runs" until the server-side code (C# in this case) has entirely completed and the resulting page is returned to the client. Once that page renders on the client, thenJavaScript runs on the client.

请注意,在服务器端代码(在本例中为 C#)完全完成并将结果页面返回给客户端之前,没有 JavaScript 真正“运行”。一旦该页面呈现在客户端上,然后JavaScript的运行在客户机上。

So in order to execute your JavaScript code, all you need to do is include it in the page being returned to the client. There are a number of ways to do this, and the options depend on whether you're using WebForms or MVC.

因此,为了执行您的 JavaScript 代码,您需要做的就是将其包含在返回给客户端的页面中。有多种方法可以做到这一点,选项取决于您使用的是 WebForms 还是 MVC。

You might use something like RegisterStartupScriptin WebForms, for example. Or, you could just have the JavaScript code exist in a PlaceHoldercontrol with Visible=falseand only make the control visible in the response which intends the JavaScript code to run. (Roughly the same method is also easily usable in MVC by just wrapping the JavaScript code in a server-side condition to determine whether to render it or not.)

例如,您可以在 WebForms 中使用类似RegisterStartupScript 的东西。或者,您可以让 JavaScript 代码存在于一个PlaceHolder控件中,Visible=false并且只使该控件在打算运行 JavaScript 代码的响应中可见。(大致相同的方法也很容易在 MVC 中使用,只需将 JavaScript 代码包装在服务器端条件中以确定是否呈现它。)

The main thing to remember is that you're not "running the JavaScript code from C#" or anything like that. There's a hard separation between server-side and client-side code. The server-side code ultimately builds the page that it sends back to the client, and that page can include JavaScript code to run on that client.

要记住的主要事情是您不是“从 C# 运行 JavaScript 代码”或类似的东西。服务器端和客户端代码之间存在硬分离。服务器端代码最终构建它发送回客户端的页面,并且该页面可以包含在该客户端上运行的 JavaScript 代码。