0

所有这些都发生在同一个用户控件中,因此不应该有所作为。

<asp:Repeater ID="rptActivity" runat="server" OnItemCreated="rptActivity_ItemCreated">
    <ItemTemplate>
        <div class="under-label">
            <div class="activity">
                <%#Eval("ActivityName")%>
                <input type="hidden" name="activityId" value='<%#Eval("ActivityId")%>' />
            </div>

            <div class="status">
                <asp:DropDownList ID="ddlStatuses" DataSourceID="SqlDataSource1"  DataTextField="Name" DataValueField="Id"  runat="server"></asp:DropDownList>
            </div>
            <div class="comment">
                <textarea name="comments" cols="35" rows="3" name="comment" style="float: left; margin: 0px 0px 0px 25px; font-family: Geneva, Arial, Helvetica, sans-serif;"><%#Eval("Comment")%></textarea>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

我在转发器的 itemcreated 事件中有以下代码:

 protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            var helper = (UpdateActivitiesHelper)e.Item.DataItem;

            DropDownList ddl = (DropDownList)FindControl("ddlStatuses");
            ddl.SelectedValue = helper.StatusId.ToString();
        }

当我尝试使用 ddl 时,它会抛出NullReferenceException.

有任何想法吗?

4

2 回答 2

3

由于您的下拉列表位于中继器内,因此请确保您引用DataItem来查找控件。

确保使用e.Item.FindControl而不是Page.FindControl-- Page.FindControlwill not find this item 因为它不会递归搜索页面

protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    var helper = (UpdateActivitiesHelper)e.Item.DataItem;
    DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
    ddl.SelectedValue = helper.StatusId.ToString();
}
于 2012-12-12T01:17:51.340 回答
1

尝试修改您的 ItemCreated eventHandler 如下所示,看看它是否有效。

protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item){
        var helper = (UpdateActivitiesHelper)e.Item.DataItem;
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
        ddl.SelectedValue = helper.StatusId.ToString();
    }
}
于 2012-12-12T01:14:57.207 回答