2

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.

3 Answers 3

1

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 + "\")'>" + ...
Sign up to request clarification or add additional context in comments.

1 Comment

you are right, if change to abc, still fails. but do you mean to write like ipr101 suggests? still fails.
0

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

Try escaping them.

1 Comment

remove single quote before and after AttachId()? No lucky.
0
<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.

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.