-1

I have a WebMethod that updates an SQL database based on parameters passed from a ASP.NET button. On success the WebMethod returns the state and is used to show or hide a div. However the Javascript condition always evaluates as "Yes" even though "No" is passed back.

Control:

<telerik:RadButton
        TabIndex="1"
        ID="Updated"
        runat="server"
        ButtonType="ToggleButton"
        ToggleType="Checkbox"
        AutoPostBack="false"
        OnClientToggleStateChanged="ButtonSelect"
        CommandArgument="Updated" 
        CommandName="Updated">
             <ToggleStates>
                  <telerik:RadButtonToggleState
                          Text="No" 
                          PrimaryIconCssClass="rbToggleRadio"> 
             </telerik:RadButtonToggleState>
             <telerik:RadButtonToggleState
                 Text="Yes" 
                 PrimaryIconCssClass="rbToggleRadioChecked"> 
             </telerik:RadButtonToggleState>
        </ToggleStates>
   </telerik:RadButton>

JQuery:

<script type="text/javascript">
    function ButtonSelect(button, args) {
        var state = args.get_currentToggleState().get_text();
        var whois = args.get_commandName();

        PageMethods.ButtonWebMethod(state, whois, onSucess, onError);

        //edited Stack Overflow Quetion here to == from =:
        function onSucess(returnstate) {
            if ((whois == "Updated") && (returnState == "Yes"))
            {
                $("#updateChoiceDIV").show();
                alert(returnstate + "Yep");
            }
            else if ((whois == "Updated") && (returnState == "No"))
            {
                $("#updateChoiceDIV").hide();
                alert(returnstate + "Nope");
            }
        }
        function onError(result) {
            alert('Cannot process your request at the moment, please try later.');
        }
    }
</script>

WebMethod:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string ButtonWebMethod(string state, string whois)
{
    string returnState = state;
    HttpContext.Current.Session["FieldName"] = whois;
    if (state == "Yes")
    {
        HttpContext.Current.Session["" + whois + ""] = "True";
    }
    else if (state == "No")
    {
        HttpContext.Current.Session["" + whois + ""] = "False";
    }
    string strConnString = WebConfigurationManager.
                                ConnectionStrings["gatorConnectionString"].
                                ConnectionString;
    try
    {
        string sql = "UPDATE [User] SET " + 
                        HttpContext.Current.Session["FieldName"] + "='" + 
                        HttpContext.Current.Session["" + whois + ""] + 
                        "' WHERE Id = '" + HttpContext.Current.Session["newID"] + 
                        "' ";
        using (SqlConnection conn = new SqlConnection(strConnString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
    catch (SqlException ex)
    {
        string msg = "Update Error:";
        msg += ex.Message;
    }
    return returnState;
}

And yes I'm not great at Javascript so be kind....

9
  • 1
    = is assignment which will always be true. == and === is equality. You have typos in your expressions.
    – Taplar
    Commented Oct 24, 2018 at 18:16
  • I have tried == too even with just returnstate yes/no and could not get it to work but I'll try again. Thanks!
    – Kyle G
    Commented Oct 24, 2018 at 18:18
  • When I add == to the JQuery I don't even get the alert anymore. The WebMethod still updates the database but no alerts function.
    – Kyle G
    Commented Oct 24, 2018 at 18:25
  • 1
    Update your question please to show what you changed. Also remember that you can add console.log(literal|variable|expression) in your logic at pretty much any point to see in the console what is going on. Ex. console.log(returnState); console.log(returnState == "Yes");
    – Taplar
    Commented Oct 24, 2018 at 18:25
  • 1
    Oh! lol, i just noticed another typo. You are passing in returnstate but checking returnState. Case is sensative on variables.
    – Taplar
    Commented Oct 24, 2018 at 18:29

1 Answer 1

1

Alright, so a couple issues were found through the comments.

  • Assignment (=) will typically return as true

This is because the result of an assignment is the value you set to it, which is typically not a falsy value.

var x;
var y;

if (x = 1) { console.log(true); }
if (y = 0) {} else { console.log(false); }

The correct way to compare values is to use === or ==. If you expect, and require, that the two variables have the same type (compairing string to string or number to number) then you should use the === version. If, however, you wish to allow the logic to coerce the values to be the same type (such as compairing 1 to "1") then you should use == and javascript will coerce them to the same type before compairing.

  • returnstate != returnState

The second issue was that the argument returnstate was being provided to the function, however the inner logic was referencing returnState which didn't match the case of the previous variable, so it would have been undefined for all the expressions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.