问题
I have a repeater control where the <%#DataBinder.Eval(Container.DataItem, "Display")%>
part doesn't show up. The code that the "Display" stores is set as follows:
item.Display = "<script type='text/javascript'>
AudioPlayer.embed('ffcedea7-4822-465f-85b6-89924f7b81fa',
{soundFile: 'http://s3.amazonaws.com/blah/af8g7fd3-1793-4b5e-92b7-9d11ad1cc19c.mp3'});
</script>";
After the page load, the audio embed file doesn't show up. The code doesn't even show up in the source. If I add a random string after the ending script tag, that random string will show up.
item.Display = "<script type='text/javascript'>
AudioPlayer.embed('ffcedea7-4822-465f-85b6-89924f7b81fa',
{soundFile: 'http://s3.amazonaws.com/blah/af8g7fd3-1793-4b5e-92b7-9d11ad1cc19c.mp3'});
</script> THIS IS THE RANDOM STRING";
So, on the page source it will have " THIS IS THE RANDOM STRING" but not the script part.
Does anyone know what is causing this issue and how it can be fixed? Thanks!
Edit: Here is the repeater code:
<asp:Repeater ID="repeaterAddable" runat="server">
<ItemTemplate>
<div class="background-white">
<div style="padding: 15px;">
<table style="width: 100%" cellspacing="5">
<tr>
<td colspan="3" align="right">
Include this? <input type="checkbox" name="include<%#DataBinder.Eval(Container.DataItem, "Index")%>" />
</td>
</tr>
<tr>
<td style="width: 30%;" valign="top">
</td>
<td style="width: 30%;" valign="top">
<div class="media">
<%#DataBinder.Eval(Container.DataItem, "Display")%>
</div>
</td>
<td style="width: 30%;" valign="top">
</td>
</tr>
</table>
</div>
</div>
<br />
</ItemTemplate>
</asp:Repeater>
回答1:
Try adding a Literal control instead, and bind its text property to the desired content. E.g:
<div class="media">
<asp:Literal runat="server" Text='<%# Eval("Display") %>' />
</div>
回答2:
Maybe you have some safe settings on your page or webconfig...
I tried to reproduce your situation in a brand new page, but it actually worked.
public class A
{
public string Display { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
var list = new List<A>();
var a = new A();
a.Display = "<script>alert('hi')</script>S<br/>";
list.Add(a);
rep.DataSource = list;
rep.DataBind();
}
And in the page
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Display") %>
</ItemTemplate>
</asp:Repeater>
Perhaps you can try to set the Display
with HttpUtility.UrlEncode
and get it with HttpUtility.UrlDecode
...
来源:https://stackoverflow.com/questions/455912/script-script-inside-a-repeater-control-code-not-showing-up-in-the-source