YES/NO javascript function asking the question even tho it shouldn´t

Viewed 59

i have this insert form that runs a verification on the name of the server, if there is a server that exist´s with that name it should trigger the javascript function and say "This server already exists, would you like to activate it?" yes/no if yes it does one thing if no it does another, while i was doing tryouts on this i noticed that even when there wasen´t a server with that name it would trigger the event non the less, ill leave here the front end and server-side code, thank you.

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

        gridServers.DataSource = dt;
        gridServers.DataBind();

        string ServerName = ServerNameADD.Value.ToString();
           
              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 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 inalterado')", true);
                    da.GetServers();
                }
            }
    }

This is Server side coding.

<asp:button Text="Insert" runat="server" OnClientClick="Confirm1()" name="txtInsertService" ID="btn_insert_service" OnClick="btn_insert_service_Click" ></asp:button>

This is the button that triggers the server side coding and then

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

This is the javascript function it self. If anyone finds out what im doing wrong, let me know.

1 Answers

Try something like the below:

Backend - Create a web method:

[WebMethod(EnableSession = true)]
public static string GetServerSideValue()
{
    return "Done";
}

Frontend - Use the below Ajax call to server-side web method:

<button type="button" class="btn btn-primary" id="btnSubmit">Get Value</button>
$("#btnSubmit").click(function () {
        debugger;        

        $.ajax({
            type: "POST",
            url: "/YourFileName.aspx/GetServerSideValue", //Here you call the method
            data: JSON.stringify({ }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data);
            },
            error: function (result) {
                alert("Error");
            }
        });
    });

N.B: Please see this for proper Ajax call - Ajax Call with ASP.NET

Related