简体   繁体   English

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

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

When a user clicks a button on ASP.net page, I need to 当用户单击ASP.net页上的按钮时,我需要

  1. Save file from asp:fileUpload in 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 asp:fileUpload中的文件保存在服务器上的文件夹中-我猜想这需要在C#中完成,例如如何正确使用ASP.NET FileUpload控件中

  2. Run a javascript function like in How to call javascript function from asp.net button click event 运行javascript函数,如如何从asp.net中的javascript函数单击按钮事件

Is there a way to combine C# and Javascript to achieve what I need? 有没有一种方法可以将C#和Javascript结合起来实现我所需要的? If not, how should I do it? 如果没有,我该怎么办?

Try using the onClientClick property of the asp:button element. 尝试使用asp:button元素的onClientClick属性。

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/zh-CN/library/system.web.ui.webcontrols.button.onclientclick.aspx

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. 请注意,直到服务器端代码(在本例中为C#)完全完成并将结果页面返回给客户端之前,实际上没有JavaScript“运行”。 Once that page renders on the client, then JavaScript runs on the client. 一旦该页面呈现在客户端上, 然后运行的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. 因此,为了执行您的JavaScript代码,您需要做的就是将其包含在返回给客户端的页面中。 There are a number of ways to do this, and the options depend on whether you're using WebForms or MVC. 有多种方法可以执行此操作,并且选项取决于您使用的是WebForms还是MVC。

You might use something like RegisterStartupScript in WebForms, for example. 例如,您可能在WebForms中使用诸如RegisterStartupScript之类的东西。 Or, you could just have the JavaScript code exist in a PlaceHolder control with Visible=false and only make the control visible in the response which intends the JavaScript code to run. 或者,您可能只是将JavaScript代码存在于Visible=falsePlaceHolder控件中,并且仅使该控件在打算运行JavaScript代码的响应中可见。 (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.) (通过将JavaScript代码包装在服务器端条件中以确定是否进行渲染,大致相同的方法也可以在MVC中轻松使用。)

The main thing to remember is that you're not "running the JavaScript code from C#" or anything like that. 要记住的主要事情是您不是在“从C#运行JavaScript代码”或类似的东西。 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. 服务器端代码最终会构建它发送回客户端的页面,并且该页面可以包含要在该客户端上运行的JavaScript代码。

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

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