2

I have the following in my aspx.

<html>
    <body>
        <input id="but" type="button" value="ClickMe" />
    </body>
</html>

Code behind

//Pageload
but.Attributes.Add("onclick", "this.parentNode.innerHTML += '<a id=""link"" href=""mylink""></a>';document.getElementById(""link"").click();");

Rendered html

<input name="ctl00$Content$btn" type="button" id="ctl00_Content_btn" value="ClickMe" 
onclick="this.parentNode.innerHTML += '&lt;a id=&quot;link&quot; href=&quot;mylink&quot;>&lt;/a>';document.getElementById(&quot;link&quot;).click();" />

How can I get the < > " characters to render properly?

5 Answers 5

4

You cannot render these characters without encoding.

From MSDN

You cannot add client-side script to a WebControl instance using the Attributes collection. To add client-side script, use the ClientScript property on the Page control.

If you insist, just render your own control.

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

2 Comments

Its funny that they say that. In the example a couple of lines further down they do exactly what they said not to do.
@DJA They did not say not to do. They say that if you do this characters will be encoded on the html tag line.
3

Thanks Aristos, this works.

Page.ClientScript.RegisterStartupScript(Me.GetType(), "butClick", "function butClick() {this.parentNode.innerHTML += '<a id=""link"" href=""mylink""></a>';document.getElementById(""link"").click();}")
but.Attributes.Add("onclick", "butClick();");

Comments

1

If you really want to prevent the encoding of parameters, you'll need to create your own customized control and override the AddAttributesToRender method. Obviously this isn't very flexible because you'll need to create a separate custom control for each type of control you use.

Fortunately, this is very easy and only needs a few lines of code so may work out. Here is the complete code needed for a customized button:

public class MyCustomButton : Button
{
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        writer.AddAttribute("onclick", "myJavascriptFunction('a string');", false); // passing false to the AddAttribute method tells it not to encode this attribute.
    }
}

Obviously, this only appends a hard-coded onclick to the end of the attributes and may interfere if an onclick has already been provided. If you wanted to take this further, you could iterate over the actual Attributes collection and add them all this way.

Comments

0

Override your Render method for the control and add this line:

base.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(new PostBackOptions(this, string.Empty)));

And on the page it should render as:

onclick="__doPostBack('ctl00$ctl00$PageContent$LocalContents$button_id', '')"

Which IE should understand as a postback reference, so it shouldn't encode it. .Net should match it up with your server side event handler

Comments

-1

.net 4.0 has inbuilt functionality. http://msdn.microsoft.com/en-us/library/system.web.httputility.javascriptstringencode.aspx - from the docs.

2 Comments

I'm stuck in .net 2.0 for this project.
use reflector or ilspy. Reflect the system.web assembly. Steal the code from there. Implement it in 2.0. Should you have difficulties let us know. :)

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.