English 中文(简体)
无法在甲型异构体控制中找到控制
原标题:Unable to find control in asp.net Repeater control

这使我 st。 我正试图在一个有活力的装设的蚊帐复发模板中找到一个检查箱。 模板是细致的,数据具有约束力,所有数据都很好,但我无法找到控制点! 任何想法?

这是重复法(我对不同风格的备选模板有类似法):

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

这就是我如何装上模板(工作罚款)

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

......最后,这是我如何设法找到检查箱(但继续 null倒)

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

我只能认为,检查箱埋在某个地方的等级中越深,但我不敢肯定,因为我认为FindControl会这样做。

这是生成的超文本:

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

问题回答

我将这一推广方法作为我的工具包的一部分:

    /// <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");

为什么你不执行<关于/code>的<编码>方法>?

例:

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

该法典将用来对EACH数据束缚检查箱进行操作,以便你能够做任何需要做的事情,而不必注意控制。 通常,这是使数据具有约束力的最佳方法,因为它将你的代码扩大到控制水平,因此,你不必在记录一级不断搜寻所有物品和硬编码搜索名称。

也许,你会把产生结果的html看到控制的确切地点。 如果你通过所有控制及其儿童控制来加以控制,你最终会发现。

我从未在前面使用过在密码背后的模板,但似乎如果你产生的超文本包含以下线:<代码><ItemTemplate>,如你所示,有些则没有适当工作。

你们是否完全使用标题/指标模板? 如果是的话,你需要检查要求项目DataBound()的模板类型。 项目DataBound()将列入每个模板,包括标题和脚本。 在要求下一个项目前将启动项目项目项目,并且由于对利息的控制没有在头部控制,因此你没有与FindControl()接触。 在称为项目DataBound()的项目类型为项目/替代项目的情况下,你只叫FindControl(),阻止将项目无效/停止退回,以寻求你的控制。

<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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签