开发者

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:

fun开发者_Go百科ction 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.


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 + "\")'>" + ...


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

Try escaping them.


<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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜