• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

C# Avoid escaping characters whilst adding attributes to web control

Hello,

When I add an attribute to my web control with as value "http://www.zzz.com/page?param1=2¶m2=66", see code below, I get in my html : "http://www.zzz.com/page?param1=2&param2=66" Which is my problem. Is anyway to avoid having the "&" replaced by "&"?
Thanks

HtmlGenericControl m_ScriptLink = new HtmlGenericControl("script");
container.Controls.Add(m_ScriptLink);
m_ScriptLink.Attributes.Add("src","http://www.zzz.com/page?param1=2¶m2=66");
0
webgour
Asked:
webgour
  • 3
  • 2
1 Solution
 
starlite551Commented:
Try it this Way..
HtmlGenericControl m_ScriptLink = new HtmlGenericControl("script");
container.Controls.Add(m_ScriptLink);
string link = "http://www.zzz.com/page?param1=2&param2=66";
if(link.Contains("&"))
{
     StringBuilder sb = new StringBuilder(link);
     sb.Replace("&","&");
     link = string.Empty;
     link = sb.ToString();   
}
m_ScriptLink.Attributes.Add("src",link);

Open in new window

0
 
starlite551Commented:
Or You can directly try this..
HtmlGenericControl m_ScriptLink = new HtmlGenericControl("script");
container.Controls.Add(m_ScriptLink);
string link = "http://www.zzz.com/page?param1=2&param2=66";
m_ScriptLink.Attributes.Add("src",link.Replace("&","&"));

Open in new window

0
 
starlite551Commented:
Sometimes you also get value such as " " you can do the same thing in that case too..
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
webgourAuthor Commented:
Thank you for your help, but that won't work, with your solution the replace occurs before the string is html encoded
0
 
Carl TawnSystems and Integration DeveloperCommented:
The problwem you will have is that all of the Html controls dump out there output using the HtmlTextWriter which will automatically escape html values. So, you're either going to have to override the Render method for the control (which you really don't want to be doing), or you can simply use a LiteralControl instead:
LiteralControl script = new LiteralControl();
script.Text = "<script src=\"http://www.abc.com/page?param=1&param2=1\"></script>";

container.Controls.Add(script);

Open in new window

0
 
webgourAuthor Commented:
I've dealt with the problem however pretty much as you mention... Thanks for your answer u get your point ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now