javascript - 使用 Javascript 在嵌套 FormView 中隐藏/显示控件

标签 javascript asp.net

编辑时我想在使用 javascript 检查 MYCheckBox 时隐藏 MyTextBox。我可以使用在 ParentFormView 中找到控制

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');

关于javascript - 使用 Javascript 在嵌套 FormView 中隐藏/显示控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958941/

相关文章:

asp.net - 无法加载文件或程序集...数组下标超出范围

c# - ASP.Net MVC 4 Bundles - 在路径名中使用变量

显示昨天和今天日期的 Javascript 代码

javascript - 使用 d3.js 的组元素 (<g>) 的渲染顺序

javascript - 使用 JavaScript 基于值的 ASP.NET 条件格式

asp.net - 为 Visual Studio 2008 推荐的操作系统

c# - 用双引号将 ASP.NET 控件括起来

javascript - 替换字符串中的下划线

javascript - 使用 getByDisplayValue Text 在 react 测试库中找不到按钮

javascript - 用单引号将字符串解析为 JSON?