如何在C#中从转发器中找到td?

时间:2013-08-09 07:51:15

标签: c# asp.net find controls repeater

我有转发器。我想显示并隐藏条件,我使用下面的代码来做:

我的 HTML 代码是:

<table cellpadding="0" cellspacing="0">
    <tr valign="top" class="list_heading">
        <td width="50%">
            Column
        </td>
        <td width="20%">
            Operation
        </td>
        <td width="19%" style="display: none;">
            And/Or
        </td>
        <td width="20%">
            Value
        </td>
        <td width="20%" id="tdvalue2Header" visible="false" runat="server">
            Value2
        </td>
        <td width="10%">
            Remove
        </td>
    </tr>
    <tbody>
        <asp:Repeater ID="rpSearchItems" runat="server" OnItemDataBound="rpSearchItems_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td style="display: none;">
                    </td>
                    <td>
                        <%# Eval("DisplayName") %>
                    </td>
                    <td>
                        <%# Eval("Operation") %>
                    </td>
                    <td style="display: none;">
                        <%# Eval("AndOr") %>
                    </td>
                    <td>
                        <%# Eval("Value") %>
                    </td>
                    <td id="tdValue2" runat="server">
                        <%# Eval("Value2") %>
                    </td>
                    <td align="center">
                        <asp:ImageButton ID="ibtnRemoveSearchItem" ImageUrl="~/Admin/Images/delete_icon.png"
                            CommandArgument=' <%# Eval("Id") %>' CssClass="RemoveUitem" ToolTip="Remove Item"
                            runat="server" OnClick="ibtnRemoveSearchItem_Click" />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </tbody>
    <tr valign="top" class="list_bottom">
        <td colspan="6">
            &nbsp;
        </td>
    </tr>
</table>  

这是item_dataBound代码:

protected void rpSearchItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = e.Item.DataItem as DataRowView;

        //HtmlTableCell tdValue2 = (HtmlTableCell)rpSearchItems.FindControl("tdValue2");
        TableCell tdValue2 = (TableCell)rpSearchItems.FindControl("tdValue2");
        if (drv.Row["Value2"].ToString() != "")
        {
            tdvalue2Header.Visible = true;
            hdfShowSecondValueColumn.Value = "11";
            if (tdValue2 != null)
            {
                tdValue2.Visible = true;
            }
        }
        else
        {
            if (hdfShowSecondValueColumn.Value != "11")
            {
                tdvalue2Header.Visible = false;
                if (tdValue2 != null)
                {
                    tdValue2.Visible = false;
                }
            }
        }
    }
}
总是 tdValue2 我得到了空,我不知道我哪里错了,请帮助我。

2 个答案:

答案 0 :(得分:6)

FindControl只能找到控件的直接子项。在这种情况下,tdValue2是转发器项的子项,而不是转发器本身。除<td runat="server">对应于服务器端控件HtmlTableCell(已在评论代码中指定)。所以正确的行应该是:

HtmlTableCell tdValue2 = (HtmlTableCell)e.Item.FindControl("tdValue2");

答案 1 :(得分:1)

要查找第一个实例,您可以使用

TableCell tdValue2 = (TableCell)rpSearchItems.Items[0].FindControl("tdValue2");