Steven Zack
Steven Zack

Reputation: 5114

passing parameter from aspx to javascript issue

I write code in asp.net backend to pass parameter, however, I found that it does not work if the parameter is so long. In the backend:

folderList.InnerHtml += "<span onclick='AttachId(" + folder.Id + ")'>" + folder.DisplayName + "</span>";

In the frontend:

function AttachId(id) {
            $("[id$='hf_FolderId']").val(id);
            $("[id$='btn_Move']").click();
        }

The parameter folder.Id="AQMkAGQ2YzBkYjkxLWZjNTYtNDAwAS1hZWY2LWQ3OWI2MDkxZTE2ZAAuAAAD82EfyZcOUkeLGnLBkE1iOgEA04D3/FOc8ES6LdlZ5/AXHgAAAQRaEQAAAA=="

If I change it to "123" it works. Any idea?

Or maybe you could let me know a better way to pass parameter from back to front. Many thanks.

Upvotes: 2

Views: 430

Answers (3)

gilly3
gilly3

Reputation: 91677

Quotes. 123 is a number so JavaScript is fine with it. If you try abc, I bet it fails as well. Add quotes to your string in the markup and it should work.

folderList.InnerHtml += "<span onclick='AttachId(\"" + folder.Id + "\")'>" + folder.DisplayName + "</span>";

Using string concatenation to generate HTML is always error prone. Remember, the JavaScript string needs to be quoted as does the HTML attribute it is contained in. Since you are using single quotes for your attribute, you must use either double quotes or htmlencoded single quotes for your JavaScript. This should work:

"... onclick='AttachId(\"" + folder.Id + "\")'>" + ...

Upvotes: 1

ipr101
ipr101

Reputation: 24236

<span onclick='AttachId(" + folder.Id + ")'>

I think this should be changed to -

<span onclick='AttachId('" + folder.Id + "')'>

So the variable is passed to the function as a string.

Upvotes: 0

rlb.usa
rlb.usa

Reputation: 15041

Actually, I believe the problem isn't with the length of your paramater, but instead with the included quote marks ".

Try escaping them.

Upvotes: 0

Related Questions