views:

3553

answers:

7

How do I delimit a Javascript databound string parameter in an anchor OnClick event?

  • I have an anchor tag in an ASP.NET Repeater control.
  • The OnClick event of the anchor contains a call to a Javascript function.
  • The Javascript funciton takes a string for it's input parameter.
  • The string parameter is populated with a databound value from the Repeater.

I need the 'double quotes' for the Container.DataItem.
I need the 'single quotes' for the OnClick.

And I still need one more delimiter (triple quotes?) for the input string parameter of the Javascript function call.

Since I can't use 'single quotes' again, how do I ensure the Javascript function knows the input parameter is a string and not an integer?

Without the extra quotes around the input string parameter, the Javascript function thinks I'm passing in an integer.

Cheers in advance for any knowledge you can drop.

The anchor:

<a id="aShowHide" onclick='ToggleDisplay(<%# DataBinder.Eval(Container.DataItem, "JobCode") %>);' >Show/Hide</a>    

and here is the Javascript:

<script language="JavaScript" type="text/javascript">
/* Shows/Hides the Jobs Div */
function ToggleDisplay(jobCode)
{
/* Each div has it's ID set dynamically ('d' plus the JobCode) */
var elem = document.getElementById('d' + jobCode);
if (elem)
{
if (elem.style.display != 'block')
{
elem.style.display = 'block';
elem.style.visibility = 'visible';
}
else
{
elem.style.display = 'none';
elem.style.visibility = 'hidden';
}
}
}
</script>
A: 

Without the extra quotes around the input string parameter, the Javascript function thinks I'm passing in an integer.

Can you do some rudimentary string function to force JavaScript into changing it into a string? Like

value = value + ""
Grant
A: 

Try putting the extra text inside the server-side script block and concatenating.

onclick='<%# "ToggleDisplay(""" &  DataBinder.Eval(Container.DataItem, "JobCode") & """);" %>'

Edit: I'm pretty sure you could just use double quotes outside the script block as well.

Shawn Simon
+4  A: 

I had recently similar problem and the only way to solve it was to use plain old HTML codes for single (&#34;) and double quotes (&#39;).

Source code was total mess of course but it worked.

Try

<a id="aShowHide" onclick='ToggleDisplay(&#34;<%# DataBinder.Eval(Container.DataItem, "JobCode") %>&#34;);'>Show/Hide</a>

or

<a id="aShowHide" onclick='ToggleDisplay(&#39;<%# DataBinder.Eval(Container.DataItem, "JobCode") %>&#39;);'>Show/Hide</a>
lubos hasko
Winna!!! Thanks Lubos. HTML codes sorted it (and yep, it was messy, but it works).Solution:onclick='ToggleDisplay("<%# DataBinder.Eval(Container.DataItem, "JOB_Code") %>");'
David HAust
A: 

I just tried using the 'Format String' parameter for the DataBinder.Eval but that didn't work either.

onclick='<%# DataBinder.Eval(Container.DataItem, "JOB_Code", "ToggleDisplay('{0}');" %>'
David HAust
A: 

Passing variable to function without single quote or double quote

<html>
<head>
</head>
<body>
<script language="javascript">
function hello(id, bu)
{
    alert(id+ bu);
}
</script>
<a href ="javascript:
var x = &#34;12&#34;;
var y = &#34;fmo&#34;;
hello(x,y)">test</a>
</body>

</html>
A: 
onclick='javascript:ToggleDisplay("<%# DataBinder.Eval(Container.DataItem, "JobCode")%> "); '

Use like above.

A: 

Guys, I'm using the same code that David said finally worked, but the server side code is not executing... it comes through as a text string instead. Arrg!

onclick='procedureChecked(&#39;<%# DataBinder.Eval(Container.DataItem, "fceventtypeid") %>&#39;);'

javascript:

function procedureChecked(fceventtypeid)
{
    alert(fceventtypeid);
}

result is a popup message with the following text:

<%# DataBinder.Eval(Container.DataItem, "fceventtypeid") %>

As you might imagine, my desired result is the evaluated expression, not the text of the expression! :)

FYI, my control is a checkbox instead of an anchor. Also, my javascript is in a separate .js file.

Thoughts?

Thanks, -Jeremy

Jeremy Ward
Try using " instead of 'This is the same as swapping single quotes for double quotes.
David HAust
I have this same issue. Swapping the quote type did not work. Jeremy, did you find a solution?
orthod0ks