Jd30814
Jd30814

Reputation: 155

How to pass comma separate string in JavaScript function from Code behind?

Code Like

In .cs page

string test = "1,2";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "OpenCenterWindow(" + test + ");", true);

JavaScript funcation

 function OpenCenterWindow(Ids) {
            alert(Ids);
}

In above I am getting alert box = 1

I have try with Encrypt and Decrypt But I am getting error like Uncaught SyntaxError: Unexpected token ) OpenCenterWindow(fBqJaxPUucc=);

But I want to 1,2 any solution?

Upvotes: 1

Views: 1844

Answers (3)

शेखर
शेखर

Reputation: 17614

Use like below

string test = "1,2";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", 
             "OpenCenterWindow('" + test + "');", true);

You need to pass the values in single quotes' or double quotest "

Edit 1

When you call a function and if you have to pass a static values you always use ' or " in javascript.

Upvotes: 1

Yatin Mistry
Yatin Mistry

Reputation: 1264

Check this work.

  <script>
        ids="1,2";
    function OpenCenterWindow(Ids) {
        alert("herer");
    alert(Ids);
    }

        OpenCenterWindow(ids);

Upvotes: 0

Manjar
Manjar

Reputation: 3268

You can do like this:

string test = "[1,2]";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "OpenCenterWindow(" + test + ");", true);

So you wil get an integer array in client side:

function OpenCenterWindow(myArray) {
     alert(Ids);
}

Upvotes: 1

Related Questions