ASP.NET JavaScript函数调用代码隐藏

时间:2017-12-28 20:57:26

标签: javascript asp.net

问题已更新。

参考: http://www.startuppirates.org/blog/gritter/

以下工作正常,点击html链接"添加常规"它显示通知如此相关.js和.css正在运行。

HTML链接

<a href="#" id="add-regular">Add regular notification</a>

的JavaScript

<script type="text/javascript">

    $('#add-regular').click(function () {

        var unique_id = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'This is a regular notice!',
            // (string | mandatory) the text inside the notification
            text: 'This will fade out after a certain amount of time.',
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: false,
            // (int | optional) the time you want it to be alive for before fading out
            time: '',
            class_name: 'gritter-info'
        });
        return false;
    });

</script>

我已经将上面的JS修改为带参数的函数,并且我试图调用它来显示代码隐藏的通知(asp.net / web-forms)但是它无效!

代码隐藏

Page.ClientScript.RegisterStartupScript(Me.GetType(), "return", "stickyalert('Property Upload', 'property data uploaded successfully', 0, 'success');", True)

的JavaScript

<script type="text/javascript">
    function stickyalert(title, text, sticky, success) {
        var unique_id = $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: title,
            // (string | mandatory) the text inside the notification
            text: text,
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: sticky,
            // (int | optional) the time you want it to be alive for before fading out
            time: '5000',
            // (string | optional) the class name you want to apply to that specific message
            class_name: 'gritter-info'
        });
        return false;
    }
</script>

1 个答案:

答案 0 :(得分:0)

您最好的选择是将您的功能置于其自身的功能之中。然后,除了从click事件调用此函数之外,还可以在页面中渲染脚本,以便在使用jquery api加载时调用该函数。

<script type="text/javascript">

    function myFunc() {
        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'This is a regular notice!',
            // (string | mandatory) the text inside the notification
            text: 'This will fade out after a certain amount of time.',
            // (string | optional) the image to display on the left
            image: '/content/gritter/images/success.png',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: false,
            // (int | optional) the time you want it to be alive for before fading out
            time: '',
            class_name: 'gritter-info'
        });
    }

    //Call it when the page loads
    $(document).ready(function() {
        myFunc();
    });

    //Call it on the click event
    $('#add-regular').click(function () {
        myFunc();
        return false;
    });
</script>