【问题标题】:Create a customized alert box in c# asp.net在 c# asp.net 中创建自定义的警报框
【发布时间】:2015-01-01 04:24:36
【问题描述】:

我想用asp控件创建一个警告框,可以吗?

例如,一个简单的脚本警报就像,

Response.Write("<script>alert('Alert Box');</script>");

在警报框上有默认按钮“确定”和“取消”,并带有文本“警报框”

现在我要做的就是在单击警报的确定按钮时调用写在各自Demo.aspx.cs 页面中的函数。

或者

是否可以在该警报框中放置一个asp:Button 控件来调用该函数?

如果有人能提供帮助,谢谢! :)

【问题讨论】:

标签: c# asp.net


【解决方案1】:

您不能在 JavaScript 警报中使用 asp:Button,在 asp.net 中制作警报或模式对话框的最佳方法是创建自己的并使其在母版页中隐藏或处于非活动状态,并在何时调用它你需要它。

您可以在GetBootstrap 中获得现成的模态或警报

更新: 这里有一些想法如何使用引导模式。

这就是我将 GetBootstrap Modal 用于 asp.net 网络表单的方式,如果你愿意,可以使用它。

以下代码用于创建自定义警报或模式框,其中包含可在后端使用的适当的 asp.net 按钮控件。 “您可以将它放在母版页中以防止重复”

<asp:Panel ID="pnlAlertBox" runat="server" style="position:absolute;top:0;> //You can make is visible or hidden -- you need to customize the style of panel
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">This is customize alertbox</h4>
                </div>
                <div class="modal-body">
                    This is the messages...
                </div>
                <div class="modal-footer">
                    <asp:Button ID="btnCancel" runat="server" CssClass="btn btn-default" Text="Cancel" />
                    <asp:Button ID="btnOk" runat="server" CssClass="btn btn-primary" Text="Ok" />
                </div>
            </div>
        </div>
</asp:Panel>

当然,您需要在母版页中包含 getboostrap .js.css 文件以显示设计。

把它放在标题中:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">

把它放在表格后面:

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

【讨论】:

  • 是的,你可以。看我的回答。
  • @sh1rts 好的,弹出警告框... ups... 但是缺少什么? asp.net 控件。
【解决方案2】:

这是 Abrar Jahin 答案的固定版本。调用 e.preventDefault() 会阻止执行默认操作,即提交表单。

以下显示点击警报,然后提交表单:-

$("#alert_button").click( function(e)
{
    alert('This is a custom alert box', 'Alert Dialog');
});

编辑:

如果你想在你的网页中调用一个特定的函数,你有几个选择:-

  • PageMethod 或 HttpHandler
  • 在您的客户端代码中,调用 ASP.NET 生成的 __doPostback 函数,并将指示要做什么的参数传递给您的服务器端代码

【讨论】:

  • 请阅读问题“我想用asp控件创建一个警告框”。
  • 感谢我确实阅读了它,我的答案已被标记为答案。
  • 恭喜,你可以预测未来。
【解决方案3】:

没有重定向页面警报代码

ScriptManager.RegisterStartupScript(this, this.GetType(), "showalert", "alert('Demo');", true);

带有重定向页面

 ScriptManager.RegisterStartupScript(this, this.GetType(), "err_msg", "alert('Demo');window.location='Demo.aspx';", true);

【讨论】:

    【解决方案4】:

    您可以在按钮的 OnClientClick 事件处理程序中使用 javascript

    <asp:Button ID="Button1" runat="server" Text="Delete User" 
         OnClientClick="return  confirm('Are you sure you want to delete this user?');" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多