iframe error when runat="server"
asp.net
Solution
When you use runat="server" - 'onload' starts being parsed as C# Event of Html Server Control, like Button.Click. You should set a name of C# event handler method in the class of your control/page (NOT JAVASCRIPT). This code will work:
<script runat="server">
void contentFrame_onLoadServer(object sender, EventArgs e)
{
if (!IsPostBack)
contentFrame.Attributes.Add("onLoad", "contentFrame_onLoadClient();");
}
</script>
<script type="text/javascript">
function contentFrame_onLoadClient() {
resizeFrame(document.getElementById('<%=contentFrame.ClientID %>'));
}
function resizeFrame(element) {
alert(element); // do your logic here
}
</script>
<iframe
runat="server"
id='contentFrame'
name='contentFrame'
width="500"
onload="contentFrame_onLoadServer"
/>
Problem
I have a iframe in one of my web pages with `runat="server"` and a javascript function assigned to the onload event. When the page renders it gives an error as `"CS1012: Too many characters in character literal"` When I remove the runat="server" attribute it works perfectly but I need the iframe to runat="server". How can I fix this? ``` <iframe id='contentFrame' name='contentFrame' runat="server" width="500" onload="resizeFrame(document.getElementById('contentFrame'))"> </iframe> ```