Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe error when runat="server"

Tags:

asp.net

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>
like image 423
Nalaka Avatar asked Dec 06 '22 10:12

Nalaka


1 Answers

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"
    />
like image 147
Philipp Munin Avatar answered Jan 10 '23 16:01

Philipp Munin