如何在asp.net中按钮点击事件调用之前和之后调用javascript函数
我在ASP.NET中创建了ButtonClick功能如下:
i have created "ButtonClick" function in ASP.NET as following:
<asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="button" CausesValidation="true" onclick="btnLogin_Click"/>
我想知道,是否可以在调用asp.net按钮之前和之后调用javascript函数点击功能... ???
i want to know, is it possible to call a javascript function before and after calling asp.net button click function...???
谢谢。
是的,这是可能的,这是一个快速示例:
要调用的Java脚本函数。
Yes it's possible, here is quick example: Java script function to call.
<script type="text/javascript">
function clientValidate() {
alert("execute before");
return true;
}
function executeAfter() {
alert("execute after");
}
</script>
这是按钮的快照
<asp:Button ID="btnLogin" runat="server" Text="Login" CausesValidation="true" OnClientClick="clientValidate()" onclick="btnLogin_Click"/>
注意属性onClientClick =clientValidate(),它会在按钮点击服务器之前触发脚本。
Notice property onClientClick="clientValidate()", it will be trigger script before button click on the server.
在服务器端:
protected void btnLogin_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, GetType(), "none", "<script>executeAfter();</script>", false);
}
注意执行executeAfter();,它会在服务器事件后触发javascript执行。
Notice executeAfter();, it will trigger javascript execution after server event.
不要忘记放置< asp:ScriptManager runat =server>< / asp:ScriptManager>
在您的aspx文件中。
Don't forget to place <asp:ScriptManager runat="server"></asp:ScriptManager>
in your aspx file.
希望它有所帮助