Reputation: 3743
All of this happens within the same user control so that shouldnt make a difference.
<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>
The i have the following code in the repeater's itemcreated event:
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
and when i try to use ddl it throws NullReferenceException
.
Any ideas?
Upvotes: 0
Views: 331
Reputation: 6554
Since your drop down list is inside the repeater, make sure you reference the DataItem
to find the control.
Make sure to use e.Item.FindControl
rather than Page.FindControl
-- Page.FindControl
will not find this item because it will not recursively search the page
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();
}
Upvotes: 3
Reputation: 4585
Try to modify your ItemCreated eventHandler like below and see if it works.
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();
}
}
Upvotes: 1