What potential side effects could arise from injecting JavaScript into the content of an updated UpdatePanel
using the below solution?
The snippet for the UpdatePanel
is as follows:
<asp:UpdatePanel>
<asp:PlaceHolder ID="pnlScriptContent" Visible="false" runat="server">
<script id="script-content">
alert('Script was loaded correctly!');
</script>
</asp:PlaceHolder>
<asp:Button OnClick="ButtonClick" OnClientClick="LoadScript()" />
</asp:UpdatePanel>
The code-behind, triggered on the click of the Button
, reveals the Panel pnlScriptContent
.
protected void ButtonClick(object sender, EventArgs args)
{
pnlScriptContent.Visible = true;
}
The JavaScript function appears as such:
var LoadScript = function() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {
eval($('#script-content').html());
};
}
I have encountered alternative methods to inject JavaScript post UpdatePanel updates, but none that permit inserting JavaScript within script
tags in the content.
Could there be a particular rationale behind this limitation? Perhaps linked to security concerns?