javascript not firing when asp.net user control loads

Posted by Jawahar on Stack Overflow See other posts from Stack Overflow or by Jawahar
Published on 2011-03-16T15:47:58Z Indexed on 2011/03/16 16:10 UTC
Read the original article Hit count: 163

Filed under:
|
|

I am trying to fire a Javascript call when an ASP.net user control to an aspx page.
The Web site allows users to add user controls to a page (similar to adding a widget etc to a page like google widgets). But when the control is added the javascript does not fire, only if the page is refreshed will it fire the javascript. IF the next time the website is accessd and the controlis still there the javascript fires too.

Do I need to use the RegisterClientScript method to register the call (setAutoTimer()) on the control load or OnPreRender event.

In the User control I have this at the start of the ascx file:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        if ($("#alarmList").length) {
            setAutoTimer();
            getData();
       }
    });

    function setAutoTimer() {
        setInterval(getData, 10000);
    }

    function getData() {
        $.ajax({
            type: "POST",
            url: "Service.asmx/getListData
            data: "{'inverterid': '" + "1'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                AjaxSucceeded(msg);
            },
            error: AjaxFailed
        });
    }

    function AjaxSucceeded(result) {
        $("#alarmList").empty();

        $("#alarmsListTemplate").tmpl(result.d)
        .appendTo("#alarmList");
    }

    function AjaxFailed(result) {
        alert(result.status + ' ' + result.statusText);
    }
</script>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ASP.NET