MrCarder
MrCarder

Reputation: 438

ASP.NET User Control Repeater.ItemDataBound Event Not Being Triggered

Event registered in aspx

<asp:Repeater ID="StepRepeater" OnItemDataBound="StepRepeater_ItemDataBound1" runat="server">

Tried with AutoEventWireUp true & false

Here's the method in the code behind:

    public void LoadSteps(Request request)
    {
        Repeater StepRepeater = new Repeater();

        StepRepeater.DataSource = request.Steps;
        StepRepeater.DataBind();
    }
    protected void StepRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
}

When stepping through, it just goes straight through "StepRepeater.DataBind();" without hitting the ItemDataBound event.

Please let me know if any additional information would help.

Upvotes: 0

Views: 5878

Answers (2)

Curtis
Curtis

Reputation: 103348

Your OnItemDataBound value doesn't match your method name.

OnItemDataBound="StepRepeater_ItemDataBound1"

protected void StepRepeater_ItemDataBound

Remove 1 from the end of OnItemDataBound or change your method name.


Also as @Adil has stated, remove the new Repeater() line:

Repeater StepRepeater = new Repeater();

UPDATE: After reading your comment on another answer regarding adding the new Repeater() line to prevent a null reference error:

Adding new Repeater() is going to create a new instance of a Repeater control, therefore not referencing the Repeater on your ASPX markup file.

If you are receiving a null reference exception, you should check that your Inherits property in your @Page directive (usually the very top line of your ASPX file) matches the class in your .aspx.cs file, and that your CodeFile property matches your .aspx.cs filename.

Upvotes: 2

Adil
Adil

Reputation: 148110

You have binded event ItemDataBound to StepRepeater in html but you are assigning that datasource of newly created repeater object and no event is attached to this repeater object.

Remove this statement

 Repeater StepRepeater = new Repeater();

You code will be

public void LoadSteps(Request request)
{
    StepRepeater.DataSource = request.Steps;
    StepRepeater.DataBind();
}

Change the name of OnItemDataBound event in html to match the code behind

<asp:Repeater ID="StepRepeater" OnItemDataBound="StepRepeater_ItemDataBound" runat="server">

Upvotes: 0

Related Questions