A LinkButton in ASP.Net has both an OnClick event and an OnCommand event. The OnClick event expects your typical EventArgs, while the OnCommand event can receive CommandEventArgs. I was about to go nuts trying to figure out how to convince my app to use CommandEventArgs in a regular OnClick event! I was like “If I can use CommandName and CommandArgument in my LinkButton tag, HOW THE HELL am I supposed to get them fed into the click event!!?!?!” Well, now I know.
If you’re using a GridView to display a list of items, you can use Container.DataItem.Index to display the items’ index in the collection. It probably works with other things like DataGrid, etc. You can also use it as an argument, thus: <asp:LinkButton … CommandArgument=’<%# Container.DataItem.Index %>’ /> – remember that it’ll come across as a string or object in the argument, though. Just do an int.Parse() or whatever.
I love building stuff. My day job has been developing web applications for a little over ten years now, with occasional desktop apps thrown in. In this blog you’ll get tips, opinions and observations on everything from data modeling to 3d modeling to plastic scale modeling. I build and sell ... Continue reading »