阻止ASP.NET在输出上编码字符串

Dou*_*son 2 asp.net encoding

当页面呈现时,如何阻止ASP.Net对List Items中的锚标记进行编码?

我有一组对象.每个对象都有一个link属性.我做了一个foreach并尝试输出BulletedList中的链接,但ASP编码了所有链接.

任何的想法?谢谢!

这是令人讨厌的代码片段.当用户选择专业时,我使用SelectedIndexChange事件清除并添加到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>"));    
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mid*_*hat 6

您可以使用转发器替换Bulletedlist

而不是PhysicianLinks.Items.Add(new ListItem("<a href=\"" + doc.Link + "\">" + doc.FullName + "</a>"));将doc.Link添加到List对象并使用它来绑定转发器.您可以在转发器itemtemplate中包含所需的任何html

就像是

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();
        }
Run Code Online (Sandbox Code Playgroud)

在ASPX中

<ul>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
            <li><a href="<%# Container.DataItem.ToString()%>"><%# Container.DataItem.ToString()%></a></li>
            </ItemTemplate>
        </asp:Repeater>
</ul>
Run Code Online (Sandbox Code Playgroud)