Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to find control in asp.net Repeater control
    text
    copied!<p>This has got me stumped. I am trying to find a checkbox in a dynamically loaded asp.net Repeater template. The template works fine and the databinding is fine and everything displays fine but I can't find the control! Any ideas?</p> <p>This is the repeater code (I have a similar one for the alternate template with a different style):</p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="template-tasks- incomplete.ascx.cs" Inherits="controls_template_tasks_incomplete" %&gt; &lt;ItemTemplate&gt; &lt;div class="task"&gt; &lt;div class="date"&gt;&lt;asp:CheckBox ID="chkIsComplete" runat="server" AutoPostBack="True" /&gt;&lt;%# DataBinder.Eval(((RepeaterItem)Container).DataItem, "DateCreated")%&gt;&lt;/div&gt; &lt;div class="description"&gt;&lt;%# DataBinder.Eval(((RepeaterItem)Container).DataItem, "TaskDescription")%&gt;&lt;/div&gt; &lt;/div&gt; &lt;/ItemTemplate&gt; </code></pre> <p>This is how I load the templates (works fine)</p> <pre><code>rptTasks.ItemTemplate = LoadTemplate("~/controls/template-tasks-incomplete.ascx"); rptTasks.AlternatingItemTemplate = LoadTemplate("~/controls/template-tasks-incomplete-alt.ascx"); </code></pre> <p>...and finally this is how I try to find the checkbox (but keeps coming up null)</p> <pre><code>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"); } } } </code></pre> <p>I can only think the checkbox is buried deeper in the hierarchy somewhere, but I'm not sure how to access it as I thought FindControl would do it.</p> <p>This is the HTML that's generated:</p> <pre><code>&lt;ItemTemplate&gt; &lt;div class="task"&gt; &lt;div class="date"&gt;&lt;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)" /&gt;23/08/2010 11:53:00 PM&lt;/div&gt; &lt;div class="description"&gt;test task&lt;/div&gt; &lt;/div&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload