Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 824
Ram Avatar asked Mar 22 '23 20:03

Ram


2 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");
like image 146
Andrei Avatar answered Apr 05 '23 16:04

Andrei


To find the first instance, you can use

TableCell tdValue2 = (TableCell)rpSearchItems.Items[0].FindControl("tdValue2");
like image 29
Christian Phillips Avatar answered Apr 05 '23 16:04

Christian Phillips