I need to retreive the DataKey of the selected row in a GridView. Here's my situation :
In a previous post ( http://forums.asp.net/t/1166794.aspx ), I asked help to add a clickable image in a gridview. The solution proposed in that post works and I did it, so I set that post resolved. Now, my problem is to access the DataKey. I know that, usually, I need to use the CommandArgument to retreive the index of the row, and then retreive different value, including the DataKey property (wich is the one I look for). Curiously, the CommandArgument passed in the OnRowCommand event is set to NULL. I trace it in the debugger, in the RowDataBound event, and it look ok at this stage; the command argument of the row having the index number that I can use further in the OnRowCommand event. But no matter if I let the default value of if I force one, I still get a null in the post back (OnRowCommand event).
So, I suppose that it my problem is related with the solution I use ( http://forums.asp.net/t/1166794.aspx ), because the other clickable column forcing a postback passes a good CommandArgument (the index of the row). So, do someone have an idea what I should do to get the index from the CommandArgument ?
Actually, the only way I found to get something from the CommandArgument parameter is to set it in the aspx page like this :
<
asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnCopy" runat="server" CausesValidation="False" CommandName="CopyButton" CommandArgument='<%# Eval("DATAFIELD1")+ ";" + Eval("DATAFIELD2")%>'>
<asp:Image ID="imgBtnCopy" runat="server" imageUrl="~/Common/Images/copy.gif" />
</asp:LinkButton>
</ItemTemplate>
In this exemple, DATAFIELD1 and DATAFIELD2 are data column. With that solution, my problem is that I dont always have the key display in the data column. Sometime, as an exemple, a table may have a primary key that I want to be hidden (ex : next number). I can put this key in the DataKey property ... but I dont know if I can send it back, with the CommandArgument, in the aspx file using a syntaxe like CommandArgument='<%... %>'
Thanks for help !
M. Schumacher