2

I have a javascript method that checks the following condition

method(selection1,selection2)
{
    if(selection1=="yes")
    {
    //Do something
    }
    if(selection2=="yes")
    {
    //Do something
    }
}

now i pass arguments from code behind like this

ClientScript.RegisterStartupScript(GetType(), "id", "method('" + selection1 + "," + "'" + selection2 + "')", true);

Here selection is a string variable

string selection1="Yes"

But the desired functionality doesn't work out. I know the javascript is correct because when i use hardcoded arguments then the javascript runs.

Kindly help. Thanks

2 Answers 2

1

Call it this way:

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", javascript:method('"+selection1+"','"+selection2+"')", true);

This will call the function and send the params as well, just be sure about the case that use in the string.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually when i tried doing it your way, i saw that i have missed a single quote in the call. That was the issue. Either way of calling is fine.(yours and mine)
I don't have that much reputation :P
0

Your code missing ' ending quotes for first string argument. Use like this

"method('" + selection1 + "', '" + selection2 + "')"

For comparison to be success the string must be exactly equal. selection1 value should be "yes" for the condition to be success

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.