Apply tablerow click to all cells but one
I have an ASP.NET page that uses a repeater to populate a grid. There will eventually be a dozen or so columns, one of which is a linkbutton that does a postback (it removes the row). The rest of the row I want to be clickable and navigate to the specified url I set up at runtime.
Here is the code for a row in the repeater:
<tr class='normal' onmouseover="this.className='highlight'" onmouseout="this.className='norm开发者_JAVA技巧al'"
onclick="location.href='<%#DataBinder.Eval(Container.DataItem, "url") %>';">
<td><%# DataBinder.Eval(Container.DataItem, "parcel")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "address")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "unitName")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "countyName")%></td>
<td><asp:LinkButton CommandName="remove" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "parcel")%>' ID="LinkButton0" runat="server">Remove</asp:LinkButton></td>
</tr>
As you can see, I have the onclick directly navigate the user to the specified url, while the LinkButton event gets handled in the parent repeater's ItemCommand handler. If I click the linkbutton, both get fired, and all hell breaks loose. Is there any way to abstain the linkbutton or linkbutton cell from having the onclick event apply to it? The only assured way I can think of to do this is set up the onclick for each table cell, but that would be pretty ugly, especially since I'm using and in the repeater.
You can add an onclick event to your LinkButton that cancels the propogation of the click event so the TR click event doesn't fire. It's been a while since I've used this outside of jQuery/MooTools but this should work for you.
onclick="window.event.cancelBubble=true;"
<asp:LinkButton CommandName="remove" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "parcel")%>' ID="LinkButton0" runat="server" onclick="window.event.cancelBubble=true;">Remove</asp:LinkButton>
精彩评论