locked
What is the best way to ask confirm from server side? RRS feed

  • Question

  • User-340993414 posted

    Hi,

    I explain my situation.

    User can create contact.  If a contact already exist in database with the same first name and the same last name I have to ask if he want to continue anyway.  If yes, contact is save, if no cancel.

    So I have to query database on the event EntityDataSource Inserting.  I know how to verify but I don't know how to ask confirm and continue or not.

    Could you help me?

    Wednesday, August 22, 2018 8:12 AM

All replies

  • User197322208 posted

    What is the best way to ask confirm from server side?

    No way. 

    On the server side is no person to answer .

    You should do on  front-end, with javascript window.confirm

    Wednesday, August 22, 2018 2:47 PM
  • User753101303 posted

    Hi,

    You can't trigger a client side dialog while your code is still running server side to create the response to the current http request (the web is based on a request/response model). Usually it is best done as early as possible ie here I would do likely something such as :
    - when submit happens cancel it and doing an Ajax call that will tell me if the first/last name is unique
    - if yes I then trigger the final submit
    - if not I ask confirmation and I trigger the final submit depending on the user response

    And so during the final submit the uniqueness problem has been dealt with already.

    Wednesday, August 22, 2018 3:42 PM
  • User-340993414 posted

    Hi,

    Can you please give me an example?

    Friday, August 24, 2018 9:47 AM
  • User-893317190 posted

    Hi miniil02,

    If you want to  the user to know that the username has existed  when he enters his username, you could use client side ajax .

    Below is my code.

     <script src="../Scripts/jquery-3.3.1.js"></script>
         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    </head>
    <body>
        <form id="form1" runat="server" class="form-inline">
            <div>
    
               
             <div class="form-group">
            <label class="col-3">Username</label>
                 <asp:TextBox ID="username" runat="server" CssClass="form-control col-6" ></asp:TextBox>
         
        </div>
    
        <div class="form-group">
            <label class="col-3">Email address</label>
            <asp:TextBox ID="email" runat="server"  CssClass="form-control col-6"></asp:TextBox>
    
        </div>
            
             <asp:Button runat="server" Text="submit" CssClass="btn btn-primary"/>
            </div>
    
    
         <script>
             $("#<%=username.ClientID%>").blur(function () {
               
                  $.ajax({
                
                        url: '/Services/myWebService.asmx/getUsername',
                        type: "post",
                        dataType: "json",
                      contentType: "application/json; charset=utf-8",
                      data: '{ "username":"'+$(this).val()+'" }',
                      success: function (data) {
                      
                              if (data.d == "No") {
                         alert("do you want to add a user who has exsited");
                     }
                        }
                    });
           
             })
         </script>

    Code behind.  To enable client side to call webservice , please uncomment the code  above the asmx file.

    //[System.Web.Script.Services.ScriptService]
      [WebMethod]
            public string[] getUsername(string username)
            {
                string[] allUsers = new string[] { "Helen", "John", "Jerry", "Nancy" };
                if (allUsers.Contains(username))
                {
                    return new string[] { "No" };
                }
                return new string[] { "Yes" };
            }

    The result.

    Best regards,

    Ackerly Xu

    Monday, August 27, 2018 2:02 AM
  • User1204533129 posted

    try this , Client side: 

    <head runat="server">
    <title>Untitled Page</title>
    <script type="text/Javascript" language ="javascript" >
    function confirm_meth()
    {
    if( confirm("Do you want to continue!Click 'YES'")==true)
    {
    document.writeln ("<b>You had click on 'YES' Button</b>");
    }
    else
    {
    document.writeln ("<b>You had clic on 'NO' Button</b>");
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnconfirm" runat="server" Font-Bold="True" ForeColor="Red" Style="z-index: 101;
    left: 272px; ; top: 208px" Text="Confrimation MsgBox"
    OnClientClick =" return confirm_meth()" Width="160px" />

    -----------------------------------------------------------------------------

    Server Side : 

    string str = "Are you sure, you want to Approve this Record?";
            this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);

    Best regards,

    kamal.

    Thursday, October 4, 2018 7:19 PM