Difficulty with LinkButton.Attribute[] usage

G

Guest

Hello,

I am trying to create an anchor tag on a page that look like this:

<a id="some_id" onSomeAction="this.href='page.aspx?a=1&b=2'">some_text</a>

I tried to accomplish this using System.Web.UI.WebControls.LinkButton object
and using the following statement:

linkbutton.Attributes ["onSomeAction"] = "this.href='page.aspx?a=1&b=2'";

When run, the '&' character gets translated to '&amp;' . As a result, the
output on the page looks like this:

<a id="some_id"
onSomeAction="this.href='page.aspx?a=1&amp;b=2'">some_text</a>


Would anyone be able to tell me how to disable the translation of the '&'
character.

Thanks
 
S

Scott Mitchell [MVP]

I am trying to create an anchor tag on a page that look like this:
<a id="some_id" onSomeAction="this.href='page.aspx?a=1&b=2'">some_text</a>

I tried to accomplish this using System.Web.UI.WebControls.LinkButton object
and using the following statement:

linkbutton.Attributes ["onSomeAction"] = "this.href='page.aspx?a=1&b=2'";

When run, the '&' character gets translated to '&amp;' . As a result, the
output on the page looks like this:

<a id="some_id"
onSomeAction="this.href='page.aspx?a=1&amp;b=2'">some_text</a>

I used Reflector to look at the code for the AttributeCollection's
Render() method. Essentially, it loops through the added attributes,
and does a writer.WriteAttribute(...) call, passing in the key and value
of the attribute, where writer is an HtmlTextWriter instance.

Now, here's the rub. The HtmlTextWriter's WriteAttribute() method takes
in as a third parameter a Boolean indicating whether or not the HTML
passed in should be HTML encoded. The AttributeCollection's Render()
method, unfortunately, passes in a hard-coded value of true. :-(

There may be an elegant workaround, but I can't think of one off the top
of my head. Sorry!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top