Reputation: 11362
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.
Upvotes: 0
Views: 1141
Reputation: 9094
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">
Upvotes: 1