How to Find td from the repeater in C#?

I am have td in repeater. i want show and hide it on condition, i have used the below code to do it:

My HTML code is:

<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>  

And here is the item_dataBound code:

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;
                }
            }
        }
    }
}

always tdValue2 i got null, i dont know where is i am wrong, please help me.

Answers


FindControl can only find direct children of the control. In this case tdValue2 is a child of a repeater item, not the repeater itself. Besides <td runat="server"> corresponds to a server-side control HtmlTableCell (as was already specified in you commented code). So the correct line should be:

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

To find the first instance, you can use

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

Need Your Help

Always receive NullReferenceException with ListBox.SelectedItem

asp.net

I have a ListBox (CompLst), and a Button with the following event:

Spring Lazy-init not working with @Resource injection

spring spring-mvc classnotfoundexception

I have an interface with real and mock implementation. for obvious reasons the mock implementation is not in the production classpath.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.