【问题标题】:How to display Bootstrap Autocloseable alert message on asp.net button click?如何在 asp.net 按钮单击时显示 Bootstrap Autocloseable 警报消息?
【发布时间】:2015-06-04 14:49:21
【问题描述】:

如何在 asp.net 按钮单击时显示 Bootstrap Autocloseable 警报消息?

代码如下:

 <div>
    <asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="btn btn-block org"
        Style="margin-top: 0px" OnClick="btnLogin_Click showalertmsg();" ValidationGroup="Login" />
</div>

使用 javascript 的可关闭代码

 <script type="text/javascript">
    function showalertmsg(message, alerttype) {

        $('#alert_placeholder').append('<div id="alertdiv" class="alert ' + alerttype + '"><a class="close" data-dismiss="alert">×</a><span>' + message + '</span></div>')

        setTimeout(function () {
            $("#alertdiv").remove();

        }, 5000);
    }
</script>

我在 asp 按钮上调用了函数名称“showalertmsg”,点击它给出错误? 编译时报错如下

Compiler Error Message: CS1026: ) expected

【问题讨论】:

  • $('#alert_placeholder').append ..... 后面缺少分号
  • 它给出编译错误:编译器错误消息:CS1026:) 预期 @sajad
  • 你加分号了吗??
  • 是的@SajadKaruthedath
  • 你也可以发布按钮点击脚本

标签: javascript asp.net twitter-bootstrap


【解决方案1】:

你不能这样调用javascript函数

OnClick="btnLogin_Click showalertmsg();"

但是

使用OnClientClick="showalertmsg();"

并在最后调用 return false 以停止回发

function showalertmsg(message, alerttype) {

        $('#alert_placeholder').append('<div id="alertdiv" class="alert ' + alerttype + '"><a class="close" data-dismiss="alert">×</a><span>' + message + '</span></div>')

        setTimeout(function () {
            $("#alertdiv").remove();

        }, 5000);

return false;
    }

【讨论】:

  • 它不显示警报消息!@Sajad
  • 请用 html 部分更新您的问题,例如 id="alert_placeholder" 的 div 和所有相关代码..以便我们回答
  • @Rohan:现在试试我的答案
【解决方案2】:

asp.net 按钮中的问题

<asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="btn btn-block org"
        Style="margin-top: 0px" OnClick="btnLogin_Click" ValidationGroup="Login" OnClientClick="showalertmsg(); return false;" />

【讨论】:

  • 它不显示警报信息! @Aravind
  • 在 OnClientClick 中需要返回 false 否则会回发。
  • 如何返回false??@Aravind
猜你喜欢
  • 2015-07-15
  • 2021-12-15
  • 2021-08-06
  • 1970-01-01
  • 2011-11-15
  • 2023-02-06
  • 1970-01-01
  • 2021-07-30
  • 2014-02-21
相关资源
最近更新 更多