【问题标题】:calling javascript/jquery function in asp.net在 asp.net 中调用 javascript/jquery 函数
【发布时间】:2013-06-22 06:18:53
【问题描述】:

我有一个使用更新面板的 aspx 页面

我放置在页面中间的某些功能比按钮客户端单击时无法调用它们。

这样的代码不起作用

案例 1:

<%--some html--%>
<asp:Button ID="btnBotton" runat="server" OnClientClick="return callfunction();" Text="Submit"/>
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
        <%--some content here--%>
        </ContentTemplate>
        <Triggers>
            <%--async trigger --%>
        </Triggers>
    </asp:UpdatePanel>
    <script type="text/javascript">
        function callfunction() {//function placed at the middle of page
            alert('hello');
        }
    </script>
    <%--some html again--%>
</asp:Content>

如果我将函数放在页面末尾而不是调用函数

案例 2:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainContent">
    <%--some html--%>
<asp:Button ID="Button1" runat="server" OnClientClick="return callfunction();" Text="Submit"/>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <%--some content here--%>
        </ContentTemplate>
        <Triggers>
            <%--async trigger --%>
        </Triggers>
    </asp:UpdatePanel>
    <%--some html again--%>


    <script type="text/javascript">
        function callfunction() {//function placed at the middle of page
            alert('hello');
        }
    </script>
</asp:Content>

所以我想知道案例 1 的问题是什么?为什么我无法调用函数

【问题讨论】:

  • 我不想从后面的代码中调用函数。很简单,在 OnClientClick 我想调用 jquery 函数

标签: javascript jquery asp.net updatepanel


【解决方案1】:

Updatepanel 会导致页面控件树的完全重新实例化,并且每个控件都会运行其生命周期事件。 JQuery 和 WebForms 通常表现不佳。我相信您在这里嵌套可能会遇到一些问题,但是如果不查看整个页面就很难判断。

【讨论】:

    【解决方案2】:

    您是否尝试过使用

    clientscript.registerstartupscript 
    

    会有帮助的。

    【讨论】:

    • 我不想从后面的代码中调用函数。很简单,在 OnClientClick 我想调用 jquery 函数
    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2011-07-21
    • 2010-09-05
    相关资源
    最近更新 更多