VB.NET: Conditional Conformation prompt on a web page RRS feed

  • Question

  • User1875103063 posted

    Here's a problem I have been puzzling over for a while... let's see what the greatest minds think about it:

    A web page has a text field and a button.  The text field is populated with today's date when the web page loads.  When the user clicks the button here's what needs to happen:

    1. If the text field's value is a date before today, an ARE YOU SURE? confirmation needs to appear.  If the user answers YES, then a report is generated using the text field's value as an input parameter.  If the user answers no, then the page is refreshed and no report is generated.
    2. If the text field's value is today or in the future, then a report is generated using the text field's value as an input parameter.

    It's the same report, the question is how to display a confirm prompt if the date is in the past.

    Javascript can be used to always display an ARE YOU SURE? confirmation on a DELETE button, but this confirmation only should appear occasionally based upon the text field's value.

    Any ideas?

    Friday, January 4, 2019 6:31 PM

All replies

  • User-943250815 posted

    You can use a javascript function to check Textbox value and return True or False, if it returns true Button.click event will be fired and you can do anything on code-behind
    On the other hand if it retuns False, Button.Click should not happen

    Friday, January 4, 2019 11:07 PM
  • User-893317190 posted

    Hi Jellicle,

    You could use js's change event of textbox.

    In addition , I suggest you had better not use the textbox's TextChanged event , because it will automatically post back.

    Below is my code , in order to let use input the right format of date , I use jquery ui's datapicker.

       <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <form id="form1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"  ></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="button1_onclick" style="display:none" />
            $(function () {
                );  // make the textbox become a datepicker
                  $("#TextBox1").datepicker(// change the format of the datepicker
                  "option", "dateFormat", "yy-mm-dd"
                var now =new Date( );
                $("#TextBox1").datepicker("setDate",now);// set the datepicker's date to now
                $("#TextBox1").change(function (e) {// bind change event of textbox1
                    var currentDate = $("#TextBox1").datepicker("getDate");// get user's date
                    var now = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
                    if (currentDate < now) {// compare use date with now
                      var result = confirm(" are you sure?");
                        if (!result) {  // if user cancel the choice , reload the page
                            window.location.href = "http://localhost:56759/JqueryPlugins";
                    // else trigger the hidden button's click event.

    Code behind.

       Protected Sub button1_onclick(sender As Object, e As EventArgs)
        End Sub

    The result.

    For more information about datepicker , you could refer to https://jqueryui.com/datepicker/

    Best regards,

    Ackerly Xu

    Monday, January 7, 2019 5:24 AM