简体   繁体   English

如何使用C#在ASP.NET中查找表格单元格

[英]How to find table cell in asp.net using c#

I want to hide an itemtemplate's data in listview using the td visibility property. 我想使用td可见性属性在listview中隐藏itemtemplate的数据。 Once I click a button it should show the data again that's within the itemtemplate. 单击按钮后,它应该再次显示itemtemplate内的数据。 However, I cannot find the td control using c# in the code behind. 但是,我无法在后面的代码中使用c#找到td控件。 Is there a way to find this control or another way to handle this? 有没有找到此控件的方法或另一种处理此控件的方法?

 <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
  <asp:Button ID="Button1" runat="server" Text="Search" OnClick="ButtonClick" />
 <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">

     <asp:View ID="View1" runat="server">
        <asp:ListView ID="SectionListView" runat="server" InsertItemPosition="FirstItem" OnPagePropertiesChanged="SectionListView_PagePropertiesChanged">

             <ItemTemplate>
                 <tr style="">
                        <td></td>
                        <td id="row1" style="visibility:hidden;" runat="server">
                                <asp:Label ID="SectionItemLabel" runat="server" Text='<%# Eval("SectionItem") %>' />
                        </td>
                 </tr>
             </ItemTemplate>

Here is part of the code for the button click: 这是单击按钮的代码的一部分:

protected void ButtonClick(object sender, EventArgs e)
    {
        var temp = (System.Web.UI.HtmlControls.HtmlTableCell)Page.Master.FindControl("MainContent").FindControl("row1");
    }

You have a couple of issues. 您有几个问题。 First, when you try to find "row1" within "MainContent", if won't find it because "row1" is actually a child of other children of "MainContent". 首先,当您尝试在“ MainContent”中查找“ row1”时,找不到它,因为“ row1”实际上是“ MainContent”的其他子级的子级。 It won't find them recursively unless you tell them to . 除非您告诉他们,否则不会递归地找到它们。

Second, since each ListViewItem within your ListView contains "row1", they are each given their own unique ID, such as SectionListView_ctrl0_row1 , SectionListView_ctrl1_row1 , etc. Because of this, you need to use FindControl() on each ListViewItem . 其次,由于ListViewItem中的每个ListViewItem都包含“ row1”,因此它们分别具有各自的唯一ID,例如SectionListView_ctrl0_row1SectionListView_ctrl1_row1等。因此,您需要在每个ListViewItem上使用FindControl()。

But, because you need to do it on each ListViewItem and because each ListViewItem contains "row1", each row will get the same property (ie all visible or all invisible). 但是,因为您需要在每个ListViewItem上执行此操作,并且由于每个ListViewItem包含“ row1”,所以每一行都将具有相同的属性(即所有可见或全部不可见)。 Here is how that could be done: 这是可以做到的:

protected void ButtonClick(object sender, EventArgs e)
{
    foreach (ListViewItem lvi in SectionListView.Items)
    {
        if (lvi.ItemType == ListViewItemType.DataItem)
        {
            HtmlTableCell row1 = (HtmlTableCell)lvi.FindControl("row1");
            row1.Style.Add("visibility", "hidden");
        }
    }
}

If you need to style each cell individually, they would each need to be named differently. 如果需要分别设置每个单元格的样式,则每个单元格都需要使用不同的名称。 It is common to attach some sort of number or database ID to the end of the ID if that is the case. 如果是这种情况,通常会在ID的末尾附加某种编号或数据库ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM