0

I have an old ASP.Net User Control with a Repeater that I need to add a button to. The problem I have is that the button doesn't appear to raise any events when clicked, although the page is posted back.

My control looks ( in relevant parts ) like this:

<ul id="MemberList">
<asp:Repeater id="MemberRepeater" runat="server">
    <ItemTemplate>
        <li>
            <%# DataBinder.Eval(Container.DataItem, "Name") %>
            ( <%# DataBinder.Eval(Container.DataItem, "Email") %> )
            <asp:Button runat="server" ID="containerButton" Text="Edit" UseSubmitBehavior="false" 
                CommandName="EditButtonPressed" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "MemberId") %>' />
        </li>

    </ItemTemplate>
</asp:Repeater>
</ul>

Then in the C# file I have this:

protected void Page_Load(object sender, EventArgs e)
{
    members = Member.GetAllAsList();
    if (!IsPostBack)
    {

        MemberRepeater.DataSource = members;
        MemberRepeater.DataBind();
        foreach (RepeaterItem itm in MemberRepeater.Items)
        {
            // Tried adding a click event to the Button itself. 
            Button editButton = (Button)(itm.FindControl("containerButton"));
            editButton.Click += editButton_Click;

        }
        // Tried adding an ItemCommand to the repeater.
        MemberRepeater.ItemCommand += MemberRepeater_ItemCommand;
    }
}

void editButton_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

void MemberRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    throw new NotImplementedException();
}

public void EditButtonPressed(object sender, CommandEventArgs e)
{
    throw new NotImplementedException();
}

Now as far as I can tell, at least one of these strategies should work, but none of those exceptions get raised and breakpoints on all three never get hit. It is, however, a while since I have worked in detail with old-fashioned ASP.Net so I wouldn't be surprised if I have forgotten some all-important step in the process.

1 Answer 1

1

If you're binding the event in the code-behind, make sure you are binding it for every request, even when IsPostback == true.

If you're binding it in the markup, you need to add the OnItemCommand property on your Repeater.

<asp:Repeater id="MemberRepeater" runat="server" OnItemCommand="MemberRepeater_ItemCommand">
Sign up to request clarification or add additional context in comments.

3 Comments

Whats wrong with MemberRepeater.ItemCommand += MemberRepeater_ItemCommand?
Events need to be hooked up on every page request, including on postbacks.
I'm sure I knew that at one time. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.