最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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

  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

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

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

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

  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

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

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

Share Improve this question edited May 23, 2017 at 12:05 CommunityBot 11 silver badge asked Jul 11, 2013 at 15:06 Yulia VYulia V 3,55910 gold badges34 silver badges66 bronze badges 1
  • Do you mean calling/executing a Javascript function on the client or on the server? – Joachim Isaksson Commented Jul 11, 2013 at 15:08
Add a ment  | 

2 Answers 2

Reset to default 4

Try using the onClientClick property of the asp:button element.

Ex, on your .aspx file:

<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)

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

  }

More info at

http://msdn.microsoft./en-us/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 pleted and the resulting page is returned to the client. Once that page renders on the client, then JavaScript runs on the client.

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.

You might use something like RegisterStartupScript in WebForms, for example. 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. (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.)

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.

发布评论

评论列表(0)

  1. 暂无评论