Execute ClientSide before ServerSide in ASP.NET

元气小坏坏 提交于 2019-12-11 16:43:48

问题


I am using ASP.NET 3.5.

When the user click on say btnSubmit I want to first execute some JavaScript code and then execute some C#/VB.NET code.

Is this possible? If so how would one do it?

Thanks in advance!


回答1:


This is very simple:

http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(Object sender, EventArgs e)
    {
        Label1.Text = "Server click handler called.";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
  <form id="form1" runat="server">
    <asp:Button ID="Button1" Runat="server" 
      OnClick="Button1_Click" 
        OnClientClick="return confirm('Ready to submit.');" 
        Text="Test Client Click" />
    <br />
    <asp:Label ID="Label1" Runat="server" text="" />
  </form>
</body>
</html>



回答2:


Have the JavaScript execute and then call a web service with xmlhttprequest from the javascript




回答3:


There is onClientClick property - check this out http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx




回答4:


Of course, you simply add an onClick event all JS code is executed before the postback.

If the code is for validation and you decide you don't want to submit you can return false and it won't post.

<asp:Button OnClientClick="" />



回答5:


Thanks for the answer guys!

To execute a function from code behind one would do this in VB.NET

 Protected Sub btnSubmit_Click(blah blah) Handles btnSubmit.Click

    ClientScript.RegisterStartupScript(Me.GetType(), "hiya", "Message()", True)
    lblLabel.Text = "Hello my name is Etienne!"

End Sub


来源:https://stackoverflow.com/questions/3415215/execute-clientside-before-serverside-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!