Three-way Quotes? Just how do I delimit a databound Javascript string parameter in ASP.NET?

Just how do I delimit a Javascript data-bound string parameter in an anchor OnClick occasion?

  • I have an anchor tag in an ASP.NET Repeater control.
  • The OnClick occasion of the anchor has a phone call to a Javascript function.
  • The Javascript function takes a string for its input parameter.
  • The string parameter is inhabited with a data-bound value from the Repeater.

I require the "double quotes" for the Container.DataItem.
I require the 'solitary quotes' for the OnClick.

And also I still require another delimiter (three-way quotes?) for the input string parameter of the Javascript function call.

Given that I can not make use of 'solitary quotes' once more, just how do I make certain the Javascript function recognizes the input parameter is a string and also not an integer?

Without the added quotes around the input string parameter, the Javascript function assumes I'm coming on an integer.

The anchor:

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

and also below is the Javascript:

<script language="JavaScript" type="text/javascript">
/* Shows/Hides the Jobs Div */
function ToggleDisplay(jobCode)
{
    /* Each div has its 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>
0
2019-05-03 18:16:43
Source Share
Answers: 3

Without the added quotes around the input string parameter, the Javascript function assumes I'm coming on an integer.

Can you do some primary string function to compel JavaScript right into transforming it right into a string? Like

value = value + ""
0
2019-05-07 17:41:40
Source

Try placing the added message inside the server-side manuscript block and also concatenating.

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

Modify : I'm rather certain you can simply make use of double quotes outside the manuscript block too.

0
2019-05-07 17:31:38
Source

I had lately comparable trouble and also the only means to address it was to make use of simple old HTML codes for solitary (&#39; ) and also double quotes (&#34; ).

Resource code was complete mess certainly yet it functioned.

Attempt

<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>
0
2019-05-07 17:10:03
Source