TheVitruvianAnalogy
TheVitruvianAnalogy

Reputation: 25

pass arguments into javascript method from code behind

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

Upvotes: 2

Views: 6351

Answers (2)

A J
A J

Reputation: 2140

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.

Upvotes: 1

999k
999k

Reputation: 6565

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

Upvotes: 0

Related Questions