Unable to find control in asp.net Repeater control

It guarded me. I am trying to find a checkbox in a dynamically loaded asp.net repeater pattern. The template works fine, and the data binding is fine, and everything displays fine, but I can not find the control! Any ideas?

This is the repeater code (I have a similar one for an alternate template with a different style):

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="template-tasks-

incomplete.ascx.cs" Inherits="controls_template_tasks_incomplete" %>
<ItemTemplate>
    <div class="task">
        <div class="date"><asp:CheckBox ID="chkIsComplete" runat="server" 
                AutoPostBack="True" /><%# DataBinder.Eval(((RepeaterItem)Container).DataItem, "DateCreated")%></div>
        <div class="description"><%# DataBinder.Eval(((RepeaterItem)Container).DataItem, "TaskDescription")%></div>
    </div>                    
</ItemTemplate>

This is how I download templates (works great)

rptTasks.ItemTemplate = LoadTemplate("~/controls/template-tasks-incomplete.ascx");
        rptTasks.AlternatingItemTemplate = LoadTemplate("~/controls/template-tasks-incomplete-alt.ascx");

... and finally, this is how I try to find the checkbox (but keeps approaching zero)

protected void rptTasks_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        CheckBox chkBoxIsComplete = (CheckBox)e.Item.FindControl("chkIsComplete");

        if (chkBoxIsComplete != null)
        {
            int taskID = (int)DataBinder.Eval(e.Item.DataItem, "TaskID");
        }
    }
}

I can only think that the checkbox is somewhere deeper in the hierarchy, but I'm not sure how to access it, as I thought FindControl would do this.

This is the HTML generated by:

<ItemTemplate>
<div class="task">
    <div class="date"><input id="ctl00_ContentPlaceHolder1_rptTasks_ctl00_ctl00_chkIsComplete" type="checkbox" name="ctl00$ContentPlaceHolder1$rptTasks$ctl00$ctl00$chkIsComplete" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$rptTasks$ctl00$ctl00$chkIsComplete\',\'\')', 0)" />23/08/2010 11:53:00 PM</div>
    <div class="description">test task</div>
</div>                    

+3
5

:

    /// <summary>
    /// find the control with the given ID, recursively below the root
    /// </summary>
    public static Control FindControlRecursive( this ControlCollection root, string id )
    {
        foreach ( Control control in root )
        {
            if ( control != null && id.Equals( control.ID, StringComparison.InvariantCultureIgnoreCase ) )
            {
                return control;
            }
            else
            {
                Control result = FindControlRecursive( control.Controls, id );
                if ( result != null )
                {
                    return result;
                }
            }
        }

        return null;
    }

:

CheckBox chkBoxIsComplete = (CheckBox)e.Item.Controls.FindControlRecursive("chkIsComplete");
+2

, OnDataBinding CheckBox?

:

<asp:CheckBox ID="chkIsComplete" runat="server"
    AutoPostBack="True" OnDataBinding="chkIsComplete_DataBinding" />

:

protected void chkIsComplete_DataBinding(object sender, System.EventArgs e)
{
    CheckBox chk = (CheckBox)(sender);
    int taskID = (int)(Eval("TaskID"));
    // do whatever it is you need to do... you can use Eval to get any record value
    // of the current row and your sender is the actually control itself.
}

, , , . , , , .

+1

, html, , . , , .

0

, , HTML- <ItemTemplate>, , - .

0

/ ? , , ItemDataBound(). ItemDataBound() , . HeaderTemplate ItemDataBound() , ItemTemplates, , FindControl(). FindControl(), , ItemDataBound(), Item/AlternatingItem, null/Nothing .

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">

<HeaderTemplate><table><tr><td>Header</td></tr></HeaderTemplate>

<ItemTemplate><tr><td><asp:button id="Button" runat="server"/></td></tr></ItemTemplate>

<FooterTemplate><tr><td>Footer</td></tr></table></FooterTemplate>

</asp:Repeater>

Protected Sub rpt_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
  If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
      Dim Button As Button = CType(e.Item.FindControl("Button"), Button)
  End If
End Sub
0

Source: https://habr.com/ru/post/1761155/


All Articles