Michel
Michel

Reputation: 23615

asp.net (4) listview gives me troubles with generating id's

i'm in a asp.net listview, in the itemtemplate.

<asp:ListView runat="server" ClientIDMode="Predictable" ClientIDRowSuffix="Texttranslations_key"ID="lvwTextitems">

This is my code in the itemtemplate:

<span runat="server" onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;"><%# Eval("Translation")%></span>
<asp:panel  runat="server" id="EditItemDiv" style="display:none">
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
</asp:TextBox>

Now i have two problems. First the span: i want the clientID of the asp:panel in the function openDiv(), so i can create some show hide functionality. However, i get this as result:

<span onclick="openDiv(&#39;&lt;%= EditItemDiv.ClientID%>&#39;)" style="width: 450px;">

my code isn't seen as code, but as plain text, and i don't know why?

Second, this line gets me a runtime error (The server tag is not well formed):

<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">

Can somebody help me out?

ps at first i used this code for the generation of the id's: "myid<%# Eval("Id")%>" but that didn't workout either...

ps i'm always getting in to trouble when using the Eval and the <%# %>, so it's probably some stupid thing (i hope)

Upvotes: 0

Views: 524

Answers (2)

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

For the first part, you definitely need to be using a binding expression:

<%# EditItemDiv.ClientID %>

The <%= %> scriptlet will have no context for each item. I assume you were "paraphrasing" the syntax you say you tried, so what didn't work before?

The "server tag is not well formed" is because you are trying to use double-quotes inside double-quotes. Change the outer to single-quotes:

Text='<%# Eval("Translation")%>'>

Basically, you can't nest similar quote types. Inline script will usually demand you use double-quotes, since single-quotes have a different meaning in c#, but you can use either double or single for markup parameter quoting. The upshot is that if you need to have inline script, use single quotes to wrap the markup parameter, which frees you to use double-quotes inside it.

If you need further single quotes in the output, e.g. to render a javascript parameter, just use &#39;. You could also use &#34; if you wanted to render double-quotes.

OnClientClick='openDiv(&#39;EditItem(<%# Eval("something") %>&#39;);'

Upvotes: 2

Waleed Al-Balooshi
Waleed Al-Balooshi

Reputation: 6406

As stated in my comment and by jamietre to fix the binding problem you need to change the code from:

Text="<%# Eval("Translation")%>"

to

Text='<%# Eval("Translation")%>'

As for the problem with the onclick of the span, it should work as you want if you just remove the runat="server" portion. I am not sure why, but it seems that adding this causes the controls to encode the onclick property.

If you need the runat="server" on the span then I will attempt to find another solution, but there are not guarantees.

Upvotes: 1

Related Questions