Search code examples
javascriptasp.netserver-side

How can i get the result of this javascript function and execute the proper stored procedure?


i have this insert form that lets the user insert the name of the server, if the server doesen´t exist it inserts, no problem and gives a javascript message confirming it was sucessful, but if the server already exist but as its _Active property = 0 it asks if he wants to activate it and what i want to do is if the user presses ok/yes it activates that server and if the user presses no it just reloads the page.ps:all the da. have there stored procedures well constructed.

protected void btn_insert_server_Click1(object sender, EventArgs e)
    {
            
           DataAccess da = new DataAccess();
           DataTable dt = new DataTable();

           string ServerName = ServerNameADD.Value.ToString();
           if(ServerName.Length > 0)
           {
              dt = da.VerifyServer(ServerName);

              if (dt.Rows.Count == 0)
              {
                da.Insert_Server(ServerName);
                dt = da.GetServers();
                gridServers.DataSource = dt;
                gridServers.DataBind();

                string message = "Servidor Inserido com sucesso.";
                string script = "window.onload = function(){ alert('";
                script += message;
                script += "')};";
                ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);

              }
               else
               {
                string message = "Servidor já existe. Deseja torna-lo ativo? .";
                string script = "window.onload = function(){ ConfirmApproval('";
                script += message;
                script += "')};";
                ClientScript.RegisterStartupScript(this.GetType(), "PopUp", script, true);

                if (true)
                {
                    da.UpdateServerToActive(ServerName);
                    string messageSuccUp = "Servidor atualizado com sucesso.";
                    string scriptSuccUp = "window.onload = function(){ alert('";
                    scriptSuccUp += messageSuccUp;
                    scriptSuccUp += "')};";
                    ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                }
                else if(false)
                {
                    da.GetServers();
                }
            }
           }
            
            
            

    }

Solution

  • Alright the way i was able to do this was, on front-end i created a onClientClick event called Confirm

    function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Este Servidor já existe, deseja ativa-lo?")) {
                confirm_value.value = "Sim";
            } else {
                confirm_value.value = "Não";
            }
            document.forms[0].appendChild(confirm_value);
        }
    

    What it says is "This Server already exists, do you wish to activate it?" and gives a yes or no option. On server-side since the onClick event is also triggered, it grabs the confirm_value (the yes or no answer) with that value it runs the if statment if its a confirmed yes value or if not it simply refreshes the gridView and the page itself.

    This is inside the onClick button event

    string confirmValue = Request.Form["confirm_value"];
                if (confirmValue == "Sim")
                {
                    da.UpdateServerToActive(ServerName);
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Servidor Ativado')", true);
                    da.GetServers();
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Status do servidor mantidos')", true);
                    da.GetServers();
                }