2012-08-01 244 views
1

我有一个包含可编辑字段和几个必填字段的页面。问题是,无论何时点击一个按钮来激活可修改的字段以进行更改,或者应用/取消这些更改时,它都会导致我的Javascript再次触发,从而在我的必填字段中附加另一个“必填字段”指示符。我希望指标只能附加在第一个负载上,而不会受到界面上单击按钮的影响。我怎样才能做到这一点?我相信我只需要我的Javascript“if”语句的正确条件,但不知道可能是什么。我研究过它并干了。在UpdatePanel中多次触发pageLoad事件

这是我的标记。您可以放心地忽略整个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> 

回答

1

把这段代码放在文档上准备好。

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

然后,它会不会被调用时,你的更新面板负载。

+1

好吧,我会死的。你知道,很久以前,我就[onLoad]和'之间的区别准备好了这篇[优秀的文章](http://encosia.com/document-ready-and-pageload-are-not-the-same/)。准备好“,并猜测我错过了关于UpdatePanel的每次更新时'onLoad'如何触发的部分。非常棒,谢谢! :) – Chiramisu 2012-08-01 21:46:03