Asp.net OnClick事件和OnClientClick事件一起使用

本文关键字:事件 一起 OnClientClick net OnClick Asp | 更新日期: 2023-09-27 18:07:17

假设我有一个创建表单和按钮保存数据在数据库中,我想显示,保存成功在使用JavaScript的div标签,但当我点击按钮,然后在事件工作,但不是2事件onclick和onClientClick ..

<here>
<asp:Button runat="server" CssClass="myButton" Text="Registration"  ID="btnRegistration" OnClick="btnRegistration_Click "  OnClientClick="return YourJavaScriptFunction();" Height="65px" Width="184px" ClientIDMode="Predictable"></asp:Button>
and   
<script type="text/javascript">
        function YourJavaScriptFunction() {
            $("#message").slideDown("slow");
            // this will prevent the postback, equivalent to: event.preventDefault();
            return false;
        }
</script>

我希望第一个数据保存然后工作onClientClick事件在一个asp:button

Asp.net OnClick事件和OnClientClick事件一起使用

您需要告诉您的Javascript代码在按钮单击回发事件发生后在页面加载时显示消息

有很多方法可以做到这一点。

一种方法是通过会话

aspx code with js:

<% if(Session["ShowMessage"] != null){ %>
   $("#message").slideDown("slow");
<% } %>

Page_Load:

Session["ShowMessage"] = null; //This will make sure that only the button will set the session state

on Button Click事件

Session["ShowMessage"] = "Not null";

另一种方法是动态添加客户端脚本到ASP。. NET网页

正如@boruchsiper建议的那样,我将在回发成功完成后调用registerclientscriptblock来选择第二个选项。