pageLoad事件在UpdatePanel中多次触发

pageLoad event firing multiple times within UpdatePanel

本文关键字:事件 UpdatePanel pageLoad      更新时间:2023-09-26

我有一个页面,其中包含一个可编辑字段和几个必需字段。问题是,每当我点击按钮激活可编辑字段进行更改时,或者当应用/取消这些更改时,它都会导致我的Javascript再次启动,这会在我的必填字段中添加另一个"必填字段"指示符。我希望指示符只附加在第一次加载时,而不受界面上单击按钮的影响。我怎样才能做到这一点?我相信我只需要为我的Javascript"if"语句提供合适的条件,但我不知道这可能是什么。我已经研究过了,结果没有结果。

这是我的标记。您可以安全地忽略整个UpdatePanel区域。您唯一需要知道的是,它中的按钮会导致每次单击都会再次运行Javascript。

谢谢!)

<asp:Content runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
        function contentPageLoad() {
            // Add "Required Field" Indicators
            if (/*NEED CONDITION*/) {
                $("h4.required").append(" <span class='"r'">*</span>");
            }
        }
    </script>
</asp:Content>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
    <!-- This is an updatable textfield. -->
    <!-- PROBLEM!: LinkButtons here cause the double firing of the javascript above. -->
    <!-- Remember that "ChildrenAsTriggers" defaults to True for UpdatePanel. -->
    <asp:UpdatePanel ID="RequestorNameUpdatePanel" runat="server">
    <ContentTemplate>
    <asp:MultiView ID="RequestorName_mv" runat="server" ActiveViewIndex="0">
        <asp:View ID="RequestorNameNormalView" runat="server">
            <div class="rightCol">
                <asp:LinkButton ID="editRequestorName" runat="server" />
                <h6 class="inlineH">Requestor's Name: <asp:Label ID="RequestorName_lbl" runat="server"></asp:Label></h6>
            </div>
        </asp:View>
        <asp:View ID="RequestorNameEditView" runat="server">
            <div class="rightCol">
                <asp:LinkButton ID="updateRequestorName" runat="server" />
                <asp:LinkButton ID="cancelRequestorName" runat="server" />
                <h6 class="inlineH">Requestor's Name: </h6>
                <asp:DropDownList ID="RequestorName_ddl" runat="server" ViewStateMode="Inherit" AutoPostBack="False" ></asp:DropDownList>
                <asp:TextBox ID="RequestorName_cb" runat="server" EnableViewState="True" AutoPostBack="False"></asp:TextBox>
                <button id="RequestorName_btn" type="button" class="toggle ui-button"></button>
            </div>
        </asp:View>
    </asp:MultiView>
    </ContentTemplate>
    </asp:UpdatePanel>
    <!-- "REQUIRED" indicator from javascript appended here -->
    <h4 class="required">First Name</h4>
    <asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
</asp:Content>

将此代码放入文档中。

$(document).ready(function() {
  $("h4.required").append(" <span class='"r'">*</span>");
});

然后,当您的更新面板加载时,它将不会被调用。