【问题标题】:Asp.net OnClick event and OnClientClick event used togetherAsp.net OnClick 事件和 OnClientClick 事件一起使用
【发布时间】:2015-11-02 22:29:34
【问题描述】:

假设我有一个创建表单并带有按钮将数据保存在数据库中,我想使用 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>

我希望保存第一个数据,然后在一个 asp:button 中处理 onClientClick 事件

【问题讨论】:

标签: javascript c# asp.net


【解决方案1】:

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

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

一种方法是通过会话

带有js的aspx代码:

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

按钮点击事件

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

另一种方法是Add Client Script Dynamically to ASP.NET Web Page

【讨论】:

  • 如何在 Click 事件中添加此代码 .. $("#message").slideDown("slow");
  • 在您将会话设置为任意字符串的代码中的 onclick 事件中。 JavaScript 将在页面重新加载时执行。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多