0

On the UI, I've got this:

enter image description here

It's a div inside a Gridview with a JSON string inside of it, and next to it is an icon that I want to use as a "copy to clipboard". I already have the CopyToClipBoard javascript method, and it works fine. What I'm having trouble with is getting the json string inside of the onclick method. Some things I've tried:

onclick='copyToClipboard("<%# Eval("LogText")%>")'
onclick="copyToClipboard('<%# Eval("LogText")%>')"

And all of the suggestions on this thread:

Passing Eval from ASPX to Javascript function as Parameter

Here's the full JSON string:

{
  "DoorId": "11574544",
  "DoorName": "out - door controller",
  "CardNumber": "65529",
  "FacilityCode": null,
  "EventID": "570924d6-94b4-45b4-aa2d-9ab4075dc668",
  "EventTimestamp": "2017-11-10 11:10:10.301",
  "Name": "Norman Bates",
  "AccessControlUserId": "15531926",
  "OrganizationSys": 1012,
  "AccessGranted": true,
  "FirstName": "Norman",
  "LastName": "Bates",
  "Email": null,
  "UserName": "normanbates",
  "Password": "Password",
  "Pin": null,
  "CreateUserIfNotExists": false,
  "CreateDoorIfNotExists": true,
  "DoorStatusSys": 476,
  "DoorLocationSys": 103979,
  "AccessIntegrationSys": 100001
}

The problem is that all of the quotes seem to be messing things up. At least I think. For example, with this:

onclick='copyToClipboard("<%# Eval("LogText")%>")'

I get this:

<img src="..." onclick="copyToClipboard("{"DoorId":"11574544","DoorName":"out - door controller","CardNumber":"65529","FacilityCode":null,"EventID":"570924d6-94b4-45b4-aa2d-9ab4075dc668","EventTimestamp":"2017-11-10 11:10:10.301","Name":"Norman Bates","AccessControlUserId":"15531926","OrganizationSys":1012,"AccessGranted":true,"FirstName":"Norman","LastName":"Bates","Email":null,"UserName":"normanbates","Password":"Password","Pin":null,"CreateUserIfNotExists":false,"CreateDoorIfNotExists":true,"DoorStatusSys":476,"DoorLocationSys":103979,"AccessIntegrationSys":100001}")">

When I click on the icon, I get this in the console:

Uncaught SyntaxError: missing ) after argument list

Not sure how to get this right.

2 Answers 2

0

Have you considered storing your json inside a javascript variable and then just writting a variable name inside that onclick event? I have had success with this, i have had to javascript serialize my json before.

Infact if it is a global variable you will not have to pass it as a parameter but can already access it on the page. why dont you drop and parse your json object on the page load as a global variable and then you can access is from anywhere.

e.g.

<asp:literal runat="server" ID="litJsonBlob" />  
<img src="..." onclick="doSomething(jsonBlob)" />

code behind

var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(LogText);

litJsonBlob.Text = string.Format("<script>var jsonBlob = JSON.parse('{0}')</script>")", serializedResult)

alternively drop your LogText straigt in the variable i dont know exactly what your set up is:

<script> var jsonBlob = JSON.parse('<%# Eval("LogText")%>'); </script>
<img src="..." onclick="doSomething(jsonBlob)" />

By the way i dont know where that json string come from but if you do drop it inside the literal control remember that it is vulnerable to javascript injection attacks.

Sign up to request clarification or add additional context in comments.

Comments

0

Try replacing the quotes in your json string with "

Eval("LogText").Replace("\"","&quot;")

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.