2

I have an ASP.NET web application and at a certain point I do this:

mycontrol.stringparameterforjscript = "document.getElementById('" + myotherparam + "').value = 'Hello'";

The problem is that this thing does not work. As you can see this sets a javascript in some event of some tag. Well when the page is redered the problem is that my parameter look like this:

<textarea onfocus="document.getElementById(&#39;myvalue&#39;).value = &#39;Hello&#39;"></textarea>

I must precise that this textbox I'm trying to set is located inside a InsertItemTemplate of a ListView and it is not so easy to intialize. For this reason I inserted my initialization code that you see inside the load event handler of my textbox. I can say you one thing: If this code referred to a text box located freely in the page and I called this piece of code from the load event handler of the page, this would work well. But I do not know how to do in this particular case.

I'm also considering the possibility to create a webcntrol to handle such a problem. I don't really know what's the best practice in this case.

3
  • Well, it's clearly getting HTML-encoded. What is "stringparameterforjscript" and how is the HTML being rendered? Commented Dec 10, 2010 at 17:11
  • How are you setting the onfocus attribute of the textarea? I'd guess that the encoding is happening through the textarea control, rather than your mycontrol.stringparameterforjscript... Commented Dec 10, 2010 at 17:12
  • @bdukes I don't think so, I pass through an ASP.NET control library property, I guess the problem is right there. There must be an escape sequence to set somewhere, it's just that I don't know it... :( Commented Dec 10, 2010 at 17:17

2 Answers 2

1

I think you might need the @ on both string literals in your assignment, and remove the slashes:

mycontrol.stringparameterforjscript = @"document.getElementById('" + myotherparam + @"').value = 'Hello'";

EDIT

How I did it:

On the .aspx:

<asp:Textbox ID="tbTest" runat="server" TextMode="MultiLine" />

In the code:

protected void Page_Load(object sender, EventArgs e)
{
    string myotherparam = "paramval";
    tbTest.Attributes.Add("onfocus", @"document.getElementById('" + myotherparam + @"').value = 'Hello'");
}

Resultant output:

<textarea name="tbTest" rows="2" cols="20" id="tbTest" onfocus="document.getElementById('paramval').value = 'Hello'"></textarea>
Sign up to request clarification or add additional context in comments.

11 Comments

It does not work, it also prints \ in the final output... But is it really such a serious issue this one?????? I Can't believe this!
Sorry my bad, the slashes should also be removed from the strings. See my edited answer. I've just tested this myself and it appears to work.
No, it does not work, always html encoded... If it does work on you, maybe it's a page encoding issue. Can you tell what page encoding did you set for your site in @page directive or in Web.config file???? utf-8? or something else?
Odd...I didn't explicitly set any encoding, it should work by default. See my updated answer for details on what I did.
This is very strange. I tried moving the code to be inside the textbox onload event handler, and it still works for me. I found this SO question, which is different but related: stackoverflow.com/questions/1361914/…. The accepted answer suggests overriding the control's render event. That seems like overkill, but if nothing else is working...
|
0

OK, I finally managed it. HTML Encoded strings recognized by the javascript engine, how's it possible? As you will see there is nothing to worry about in what happens.

Comments

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.