【问题标题】:Using Bootbox prompt in ASP.NET webforms在 ASP.NET 网络表单中使用 Bootbox 提示符
【发布时间】:2019-01-16 08:47:50
【问题描述】:

我无法弄清楚如何在我的 ASP.NET 网络表单中使用 Bootbox 的提示功能。目前我正在使用默认的 JS 提示,如下所示:

<script language='javascript'>
    function defaultval() {
        var pmt = prompt('Please enter a name for the template.',
            document.getElementById('HiddenFieldCurrentTemplate').value);
        return pmt;
    }
</script>

<asp:Button ID="btnSaveTemplate" runat="server" CssClass="btn-primary"  OnClientClick="HiddenField1.value = defaultval()" Text="Save as Template" />

基本上我需要帮助来用 Bootbox 的提示替换默认的 JS 提示,并将提示返回的值分配给一个隐藏字段,就像我目前使用 OnClientClick="HiddenField1.value = defaultval()"

【问题讨论】:

  • 试试 bootbox.confirm
  • 对话框只是弹出,无论如何都会发生回发:-(
  • 你能分享你的代码吗?
  • 正如我们在documentation 中指出的那样,您不能使用 bootbox.prompt 作为原生提示的替代品。我需要一些时间来创建答案,但基本上,您需要阻止回发,调用提示函数,然后如果结果符合您的条件,则手动触发回发 (like so)。

标签: javascript asp.net .net webforms bootbox


【解决方案1】:

试试下面的代码 sn-p。

使用 asp.net web 控件代替我在下面使用的 html 控件

function defaultval() {
 bootbox.prompt("This is the default prompt!", function(result){ $("#HiddenField1").val(result);
$('#form1').submit();
 });
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" />
<form id="form1" action="" method="post">
<input type='submit' onclick='return defaultval()' id='btnSubmit' name='btnSubmit' value="Save as Template" />

<input type='hidden' id='HiddenField1' name='HiddenField1' />
</form1>

[编辑]

请查找 ASP.NET 版本

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" />

</head>
<body>
 <form id="form1" runat="server">
     <asp:Button runat="server" ID="btnSubmit"  OnClick="btnSubmit_Click" OnClientClick='return defaultval()' Text="Save as Template" />
     <asp:HiddenField ID="HiddenField1" runat="server" />
</form>
    <script>
        function defaultval() {
            event.preventDefault();
            bootbox.prompt("This is the default prompt!", function (result) {
                if (result != null && result!='') {
                    $("#HiddenField1").val(result);
                    $("#<%=btnSubmit.ClientID%>").click();
                } else {
                    alert('please enter something in textbox');
                    return false;
                }
            });
           
        }
    
    </script>
</body>
</html>
C# 代码
   protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Write(HiddenField1.Value);
        }

【讨论】:

  • 感谢您的回复。当警报显示它时,该值确实分配给了隐藏字段,但不幸的是,当我使用 asp.net 按钮控件时没有发生回发
  • 你之前说过,你不想回发。我认为您想在后面的代码中使用分配的隐藏值。 , 对?我编辑了代码。
  • 不。不工作。一旦我有机会在提示中输入一个值并将该值保存到隐藏字段中,我确实想要回发,因为我不处理服务器端隐藏字段中的值......对不起,我想要清楚这一点。另外,如果我没记错的话,我认为强制表单提交适用于 html 表单而不是 web 表单。
  • 网络表单呈现为 HTML 表单。网络表单上的强制提交工作也是如此。进一步添加这个,你可以检查 bootbox 的回调功能
  • 按原样复制并粘贴您的代码。单击按钮时,现在什么也没有发生。只是发生了回发。 Firefox 中的浏览器控制台显示以下错误:ReferenceError: event is not defined[了解更多] WebForm2:29:9 defaultval localhost:4359/WebForm2:29:9 onclick localhost:4359/WebForm2:1:8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 2021-09-21
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多