无法在我的孩子中继器内找到一个Div

时间:2016-06-17 19:39:28

标签: asp.net repeater

我试图改变我的Div的Visible,但我无法找到它。它似乎是因为它在另一个中继器内部。

<asp:Repeater ID="uxPesquisaList" runat="server" OnItemDataBound="uxQuestList_ItemDataBound">
    <ItemTemplate>
        <tr>
            <td>
                <strong>
                    <%# DataBinder.Eval(Container.DataItem,"Descricao")%></strong>
            </td>
        </tr>
        <%--    Listagem de Respostas --%>
        <asp:Repeater ID="uxRespList" runat="server">
            <ItemTemplate>
                <tr>
                    <div id="uxRespostaText" visible="false" runat="server"> ***I want to display this Div***
                        <td>
                            <asp:TextBox ID="uxResposta" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Descricao")%>' />
                        </td>
                    </div>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

我的代码背后

protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptRespostas = (Repeater)(e.Item.FindControl("uxRespList"));

        QuestionarioPergunta pergunta = (QuestionarioPergunta)e.Item.DataItem;

        System.Web.UI.HtmlControls.HtmlContainerControl uxRespostaText = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("uxRespostaText"); **I try this, but it always return NullExpection**

        if (pergunta.TipoPergunta == "Dissertativa")
        {
            uxRespostaText.Visible = true;
        }

        rptRespostas.DataSource = ctx.QuestionarioRespostas.Where(x => x.PergId == pergunta.Id).ToList();
        rptRespostas.DataBind();
    }
}

1 个答案:

答案 0 :(得分:1)

您必须在内部Repeater的每个项目中查找TextBox。您可以在标记中设置ItemDataBound uxRespList事件处理程序:

<asp:Repeater ID="uxRespList" runat="server" OnItemDataBound="uxRespList_ItemDataBound">

在代码隐藏中,您将外部Repeater的事件处理程序中的内部Repeater的数据绑定(正如您在代码中所做的那样):

protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptRespostas = e.Item.FindControl("uxRespList") as Repeater;
        ...

        rptRespostas.DataSource = ...
        rptRespostas.DataBind();
    }
}

在绑定数据后处理每个内部Repeater项目:

protected void uxRespList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        TextBox uxRespostaText = e.Item.FindControl("uxRespostaText") as TextBox;
        ...
    }
}