datakey

Get DataKey values in GridView RowCommand

不想你离开。 提交于 2019-12-17 08:29:12
问题 I have a GridView with an associated DataKey , which is the item ID. How do I retrieve that value inside the RowCommand event? This seems to work, but I don't like the cast to LinkButton (what if some other command is firing the event?), and I'm not too confident about the NamingContainer bit. LinkButton lb = (LinkButton)e.CommandSource; GridViewRow gvr = (GridViewRow)lb.NamingContainer; int id = (int)grid.DataKeys[gvr.RowIndex].Value; I'm aware that I could instead pass that ID as the

Get DataKey values in GridView RowCommand

蓝咒 提交于 2019-11-27 06:39:59
I have a GridView with an associated DataKey , which is the item ID. How do I retrieve that value inside the RowCommand event? This seems to work, but I don't like the cast to LinkButton (what if some other command is firing the event?), and I'm not too confident about the NamingContainer bit. LinkButton lb = (LinkButton)e.CommandSource; GridViewRow gvr = (GridViewRow)lb.NamingContainer; int id = (int)grid.DataKeys[gvr.RowIndex].Value; I'm aware that I could instead pass that ID as the CommandArgument , but I chose to use DataKey to give me more flexibility. I'm also aware that it's possible