simple but baffling javascript prob

G

Guest

hi folks,
I'm using the .Net framework 1.1 with ASP.net and C#:

this is what I'm trying to do...

I've got a repeater template working that creates a hyperlink with a
javascript function call embedded inside it
when the user clicks on the link, it populates a textbox with a value
specific to that link (i.e a person's ID)...I can make this work in a
standalone browser window, but I'm trying to stuff this functionality at run
time into the repeater's template like so:


<asp:Repeater id="repPersonnel" runat="server">
<HeaderTemplate>
<h1>Returned Names</h1>
</HeaderTemplate>

<ItemTemplate>
<a href='#' onClick="passValue('" +
<%# DataBinder.Eval(Container.DataItem,"file_no") %> +
"');return false" >
<%# DataBinder.Eval(Container.DataItem,"sort_nm") %>
</a>
<p />
</ItemTemplate>



I realize this is a bit opaque but the output should look like this:

<input type="text" id="txtChosenvalue" name="txtChosenvalue" />
<p/>
<a href="" onClick="passValue('1000234');return false">Adams, Fred</a>
<p/>
<a href="" onClick="passValue('2000234');return false">Williams, Barry</a>

the passValue function looks like this:
function passValue(theid)
{
document.getElementById("txtChosenvalue").value=theid;
}

the error I'm getting at run time in the browser window is a Javascript error:
Unterminated string constant

and it's to do with trying to stuff the dynamically returned id as a string
constant into the javascript function call as a parameter, inside the
hyperlink.

the problem is, I can't see how it's unterminated, I've got the return value
safely cradeled between two single quotes like so:

<a href='#' onClick="passValue('" +
<%# DataBinder.Eval(Container.DataItem,"file_no") %> +
"');return false" >


any ideas?

Thanks and regards in advance,
CharlesA
 
G

Guest

I've got it to work! if anyone is interested it's because I was labouring
under the misapprehension that I had to concatenate the whole thing the way
you would with normal string manipulation... but instead of this:
<a href='#' onClick=
"passValue('"
+
<% # DataBinder.Eval(Container.DataItem,"file_no")%> #
+
"');return false" >

<%# DataBinder.Eval(Container.DataItem,"sort_nm") %> </a>



what is required is just this:
<a href='#' onClick=
"passValue('<%# DataBinder.Eval(Container.DataItem,"file_no")%> ');return
false" >
<%# DataBinder.Eval(Container.DataItem,"sort_nm") %> </a>

you have to take out the + concatenation operator and the extra quotes and
just give what the HTML stream is literally expecting

I only managed to figure it out because my mind went into 'think clearly
about how your formulate your problem and explain it to others if you're
going to do a post' mode. So it was helpful to post it anyway

Cheers,
CharlesA
 
G

Gozirra

This will work. Just a little tip, look at the source of the rendered
page and you can usually see what the problem is in cases like this.

<asp:Repeater id=Repeater1 runat="server">
<ItemTemplate>
<a href='javascript:passValue("
<%# DataBinder.Eval(Container.DataItem,"file_no") %>")'>
<%# DataBinder.Eval(Container.DataItem,"sort_nm")
%><a/><br>
</ItemTemplate>
</asp:Repeater>

If you had looked at the rendered output from your statements you would
have seen this:
<a href='#' onClick="passValue('" + Fred + "');return false" >1 <a/>

Fred (my bs data) would be the unterminated string causing the problem.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top