views:

76

answers:

3

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>"));    
            }
        }
    }
A: 

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;

Bryan
+1  A: 

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>
Midhat
A: 

I ended up switching to a repeater, but my boss gave me the idea first so I'm answering my own question.

Darkwater23