简体   繁体   中英

Using Bootbox prompt in ASP.NET webforms

I'm having trouble figuring out how to use the Bootbox's prompt functionality in my ASP.NET webforms.Currently I'm using the default JS prompt as follows:

<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" />

Basically I need help in replacing the default JS prompt with Bootbox's prompt and assign the value returned by the prompt to a hidden field like I currently doing with OnClientClick="HiddenField1.value = defaultval()"

Try below code snippet.

Use asp.net web controls instead of html control i used below

 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> 

[Edit]

Please find the ASP.NET version

 <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# Code
  protected void btnSubmit_Click(object sender, EventArgs e) { Response.Write(HiddenField1.Value); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM