c0mrade
c0mrade

Reputation: 87

onclick not being fired in ListView

I have a button within the ItemTemplate of my ListView:

<asp:ListView ID="notificiationsList" runat="server">
<ItemTemplate>
<button type="submit" commandargument='<%# Eval("offerID") %>' onclick="Accept_Click" runat="server" >Accept</button>
</ItemTemplate>
</ListView>

Then I have a breakpoint in my code:

protected void Accept_Click(object sender, EventArgs e)
{
  .... // breakpoint here
}

However when I debug the page does nothing and it doesn't reach the breakpoint for some reason?

Does anyone understand what I am doing wrong?

Upvotes: 0

Views: 612

Answers (2)

Milton
Milton

Reputation: 968

I had the same issue and was a statement in the pageload that causes the ListView to be binded again.
This causes the initial event lost.
Check the Page_Load just to be sure :)

HTH, Milton

Upvotes: 0

SSharp
SSharp

Reputation: 182

I'm not entirely sure how you are binding your ListView. I created the following code with a few tweaks to what you have above.

<asp:ListView ID="lvNotification" runat="server">
     <ItemTemplate>
         <asp:LinkButton ID="lbAccept" runat="server" OnClick="Accept_Click" CommandArgument="test" Text="Accept" />
    </ItemTemplate>
</asp:ListView>

Binding the ListView:

List<string> tL = new List<string>(){ "this", "and", "that"};

lvNotification.DataSource = tL;
lvNotification.DataBind();

And I reused your click code:

protected void Accept_Click(object sender, EventArgs e)
{ 
    // breakpoint here
}

I was able to hit the breakpoint without issue.

Upvotes: 1

Related Questions