SHOW CONFIRMATION MESSAGE YES OR NO IN ASP.NET

Maria Susan

Total Post:29

Points:107
Posted by  Maria Susan
C# 
 1048  View(s)
Ratings:
Rate this:

I have drop-down box where users can select Yes or No, if user selects Yes from the drop-down then i want to show a confirmation box that shows Yes or No option. If only user selects Yes in the confirmation box then i want to proceed calling my other function which makes an update to the backend database. If user selects No in the confirmation box then i don't want to proceed and cancel the operation. Here is my drop-down code: 

OnSelectedIndexChanged="CheckDropDownSelection"
                        runat="server" AppendDataBoundItems="true" Height="16px">
                        <asp:ListItem Text="-- Please Selet --" Value="-- Please Selet --"></asp:ListItem>
                        <asp:ListItem Text="YES" Value="YES"></asp:ListItem>
                        <asp:ListItem Text="NO" Value="NO"></asp:ListItem>
                    </asp:DropDownList>
here is my code behind:
 
protected void CheckDropDownSelection(Object sender, EventArgs e)
    {
        if (ddl_CloseTask.SelectedValue == "YES")
        {
            CloseTask();
        }
        else
        {
        }
    }
 
    protected void CloseTask()
    {
        // here is where i close the task
    }

  1. sandy taylor

    Post:46

    Points:324
    Re: Show Confirmation Message Yes or No in Asp.Net

    The code has too many Yes/No. I hope it won't confuse to user - 

    If a user selects YES in DropDownList, a Confirmation Message will be prompted. 

    If the user selects YES in Confirmation Message, DropDownList will post back to server. 

    <asp:DropDownList ID="ddl_CloseTask" Width="157px"    AutoPostBack="true" 
       OnSelectedIndexChanged="CheckDropDownSelection"   runat="server"
       AppendDataBoundItems="true" Height="16px">
       <asp:ListItem Text="-- Please Selet --" Value="-- Please Selet --"></asp:ListItem>
       <asp:ListItem Text="YES" Value="YES"></asp:ListItem>
       <asp:ListItem Text="NO" Value="NO"></asp:ListItem>
    </asp:DropDownList> 
    <script type="text/javascript">
        var selectlistId = '<%= ddl_CloseTask.ClientID %>',
            selectlist = document.getElementById(selectlistId); 
        selectlist.onchange = function() {
            if (selectlist.options[selectlist.selectedIndex].value == "YES") {
                if (confirm("Are you sure you want to do this?")) {
                    __doPostBack(selectlistId, '');
                }
            }
        };
    </script>

      Modified On Apr-07-2018 05:32:14 AM
  1. Ankit Singh

    Post:341

    Points:2389
    Re: Show Confirmation Message Yes or No in Asp.Net

    This is how you can achieve with sever and client side code 

    attach event of client code mostly in page_load 

    yourDropDownList.Attributes["onChange"] = "jsFunction(this);";

    Client script 

    function jsFunction(mlist){
      var myselect = mlist;
      if(myselect.options[myselect.selectedIndex].value == "YES")
      {
        if(confirm("Delete item!"))
        {
          $.ajax({
           type:"POST",
             url: window.location.pathname + "/CloseTask";,
               data: dataString,
              contentType:"application/json; charset=utf-8",
                dataType:"json",
             error:
                      function(XMLHttpRequest, textStatus, errorThrown) {
                             $(errorlableid).show();
                             $(errorlableid).html("Error");
                      },
             success:
                 function(result) {
     
                    }
             }
             }
          });
        }
      }
    }

    serverside code 

    [WebMethod]
    protected void CloseTask()
    {
        //code to close task
     
    }

      Modified On Apr-07-2018 05:31:39 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!