HIde / 在嵌套的 FormView 中使用 Javascript 显示控件

HIde / Show Controls in nested FormView using Javascript

本文关键字:Javascript 显示 控件 FormView 嵌套 HIde      更新时间:2023-09-26

编辑 我想在使用javascript检查MYCheckBox时隐藏MyTextBox。我能够使用

var MyTextboxINParentFV= document.getElementById('<%=ParentFormView.FindControl("MyTextboxINParentFV").ClientID %>');

但是到目前为止,我无法在ChildFormView中获得控件,有人可以帮助我吗?

<asp:formview ID="ParentFormview" runat="server">
   <ItemTemplate>
                <asp:TextBox  ID="MyTextBoxInParentFV" runat="server"></asp:TextBox>
                 <asp:CheckBox ID="MYCheckBoxInParentFV" runat="server"></asp:CheckBox>    
       <asp:FormView ID ="ChildFormView1" runat="server">
           <ItemTemplate>
                <asp:TextBox  ID="MyTextBoxInChildFV" runat="server"></asp:TextBox>
                 <asp:CheckBox ID="MYCheckBoxInChildFV" runat="server"></asp:CheckBox>
           </ItemTemplate>
       </asp:FormView>
   </ItemTemplate>
     <EditItemTemplate>
         <asp:TextBox  ID="MyTextBoxInParentFv" runat="server"></asp:TextBox>
                 <asp:CheckBox ID="MYCheckBoxInParentFV" runat="server"></asp:CheckBox> 
         <asp:FormView ID="ChildFormView2" runat="server">
             <EditItemTemplate>
                <asp:TextBox  ID="MyTextBoxInChildFV" runat="server"></asp:TextBox>
                 <asp:CheckBox ID="MYCheckBoxInChildFV" runat="server"></asp:CheckBox>
             </EditItemTemplate>            
         </asp:FormView>
    </EditItemTemplate>   
</asp:formview>

您可以为内部 FormView 的控件设置类名:

<asp:FormView ID ="ChildFormView1" runat="server">
   <ItemTemplate>
       <asp:TextBox  ID="MyTextBoxInChildFV" runat="server" CssClass="childTextBox"></asp:TextBox>
       <asp:CheckBox ID="MYCheckBoxInChildFV" runat="server" CssClass="childCheckBox"></asp:CheckBox>
   </ItemTemplate>
</asp:FormView>

并使用该类名检索它们:

var childFV = document.getElementById('<%=ParentFormView.FindControl("ChildFormView1").ClientID  %>');
var childTextBoxes = childFV.getElementsByClassName('childTextBox');
var childCheckBoxes = childFV.getElementsByClassName('childCheckBox');