0

I am creating a new literal control from code behind, I add standard html into it which works fine. I try to add <%= SomeDiv.ClientID %> into this literal control but it quite literally prints into source exactly whats in code behind.

Code behind:

new LiteralControl("<div id='" + paneID.Replace(" ", "") + "_Expand' runat='server' onclick='ChangePanelHeader(&#39;<%= " + paneID.Replace(" ", "") + "_Expand.ClientID %>&#39;" + ", " + "&#39;<%= " + paneID.Replace(" ", "") + "_cpe.ClientID %>&#39;)' class='rhsCollapsiblePanelsHeader'>" + paneTitle + "<br /><br /></div>" + paneContent)

In Browser Source once page is loaded:

<div id='Tellafriend_Expand' runat='server' onclick='ChangePanelHeader(&#39;<%= Tellafriend_Expand.ClientID %>&#39;, &#39;<%= Tellafriend_cpe.ClientID %>&#39;)' class='rhsCollapsiblePanelsHeader'>Tell a friend<br /><br /></div>

Wanted result:

<div id='Tellafriend_Expand' runat='server' onclick='ChangePanelHeader("MyTelAFriendID_Expand", "MyTelAFriendID_cpe") class='rhsCollapsiblePanelsHeader'>Tell a friend<br /><br /></div>

Firefox syntax error:

syntax error
[Break On This Error]   

ChangePanelHeader(<%= Tellafriend_Expand.ClientID %>, <%= Tellafriend_cpe.Cli...

default.aspx (line 1, col 20)
0

1 Answer 1

2

(After my initial "Huh?") I notice you're getting your server-side code to output ids that are not wrapped in quotes. In JS those ids likely need to be treated as strings, so try this:

ChangePanelHeader("<%= Tellafriend_Expand.ClientID %>", ...);
// add quotes ----^----------------------------------^

(If that's not it, well, I'll wait for the question rewrite promised in your comment.)

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

5 Comments

Thanks mate, sorry its 3 am in the morning in the uk. I have rewritten it let me know if you require anything else.
You've embedded the <%= within your server-side string literals, so that's why it all ends up in the source the browser sees. I haven't really done anything with ASP code-behind so I'm not sure how to achieve the effect you want - I gather you want to dynamically generate the name of an object and then get the .ClientID of that object?
Yep I have just noticed I can get it in code behind using Object.ClientID I will see what this produces
Instead of <%= object.ClientID %> you can just do Object.ClientID in code behind, also in stead of &#39; i just did \" I should of been escaping characters from the start.
I nearly updated my answer to suggest exactly that, but it looked to me like you were building the name of the object by taking the string from paneID.Replace() and adding "_expand" on the end, and I wasn't sure about how to deal with that part.

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.