2

How can I stop ASP.Net from encoding anchor tags in List Items when the page renders?

I have a collection of objects. Each object has a link property. I did a foreach and tried to output the links in a BulletedList, but ASP encoded all the links.

Any idea? Thanks!

Here's the offending snippet of code. When the user picks a specialty, I use the SelectedIndexChange event to clear and add links to the BulletedList:

if (SpecialtyList.SelectedIndex > 0)
    {
        PhysicianLinks.Items.Clear();

        foreach (Physician doc in docs)
        {
            if (doc.Specialties.Contains(SpecialtyList.SelectedValue))
            {
                PhysicianLinks.Items.Add(new ListItem("<a href=\"" + doc.Link + "\">" + doc.FullName + "</a>"));    
            }
        }
    }
3
  • @Midhat God forbids you to down-vote or vote to close...?! Commented May 11, 2010 at 20:58
  • @Midhat I've added a snippet, but I thought it was pretty clear. Sorry! Commented May 11, 2010 at 21:15
  • PhysicianLinks in a BulletedList control. SpecialtyList is a dropdown control. Physician is just a custom class I created that contains a doctor's full name, specialties and URL. Commented May 11, 2010 at 21:16

2 Answers 2

6

You can replace the Bulletedlist with a repeater

Instead of PhysicianLinks.Items.Add(new ListItem("<a href=\"" + doc.Link + "\">" + doc.FullName + "</a>")); , add doc.Link to a List Object and use it to bind the repeater. You can include any html you want in the repeater itemtemplate

Something like

List<string> docLinks=new List<string>();
    if (SpecialtyList.SelectedIndex > 0)
        {


            foreach (Physician doc in docs)
            {
                if (doc.Specialties.Contains(SpecialtyList.SelectedValue))
                {
                  docLinks.add(doc.Link) ;
               }
            }
            Repeater1.DataSource=docLinks;
            Repeater1.DataBind();
        }

And In ASPX

<ul>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
            <li><a href="<%# Container.DataItem.ToString()%>"><%# Container.DataItem.ToString()%></a></li>
            </ItemTemplate>
        </asp:Repeater>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

Give us an example... the object's exact type/value and what is being rendered to HTML.

If you're talking about ASP.NET changing &amp; to &amp;amp; ... you can sometimes get around this sort of thing by using the ASCII code version instead of the HTML shortcut: &#38;

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.