如何从ScriptManager.RegisterStartupScript调用javascript函数?

时间:2015-02-02 10:08:45

标签: javascript asp.net vb.net

我有以下javascript代码:

<script type="text/javascript" language="javascript">
        $(document).ready(
            function () {
                // THIS IS FOR HIDE ALL DETAILS ROW
                $(".SUBDIV table tr:not(:first-child)").not("tr tr").hide();
                $(".SUBDIV .btncolexp").click(function () {
                    $(this).closest('tr').next('tr').toggle();
                    //this is for change img of btncolexp button
                    if ($(this).attr('class').toString() == "btncolexp collapse") {
                        $(this).addClass('expand');
                        $(this).removeClass('collapse');
                    }
                    else {
                        $(this).removeClass('expand');
                        $(this).addClass('collapse');
                    }
                });

                function expand_all() {
                    $(this).closest('tr').next('tr').toggle();
                    };
            });

    </script>

我想通过代码隐藏调用expand_all函数。

我知道我可以使用这样的东西,但它不起作用,我不理解使用的参数:

ScriptManager.RegisterStartupScript(Me, GetType(String), "Error", "expand_all();", True)
你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

您的方法expand_all仅存在于$(document).ready(...)内的函数范围内,为了让您从ScriptManager.RegisterStartupScript调用它,它需要在窗口级别,只需移动它功能在$(document).ready(...)

之外
<script type="text/javascript" language="javascript">
    $(document).ready(
        function () {
           ....                
        });

    function expand_all() {
           $(this).closest('tr').next('tr').toggle();
    };
</script>

答案 1 :(得分:1)

因为您在匿名expand_all事件上下文中定义了$.ready函数。把你的代码放在外面,它应该工作。

function expand_all(){
    alert('test B');
}
$(document).ready(
    function () {
        // this won't work
        function expand_all() {
            alert('test A');
        };
    });


// will show test B
expand_all();

检查一下:

http://jsfiddle.net/jrrymg0g/