开发者

ASP.NET Gridview with Select and its ID being visible or hidden

Using a GridView, I have it so that when the "select" link for the row is clicked, the item (in this case "category") is put into a text box to be renamed or edited in some way.

However, in order to save it, I need the ID of the selected row. I can get the ID if I show it as a column and do this in the CategoryGridView_SelectedIndexChanged event:

GridViewRow row = CategoryGridView.SelectedRow;

Session["SelectedID"] = row.Cells[2].Text;

However, I don't like that I have to display the ID column because of aesthetic reasons and also I just don't like people seeing my IDs.

On the other hand, if I hide the ID column in t开发者_如何转开发he GridView, how can I associate the Category text back to the original row in the Database?


for this purpose you can use DataKeyNames and DataKeys properties of grid view


You can use DataKeys property to associate the id's with a particular row and then retain the value in postback or else where.

Check this link:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx


Here is a short tutorial that deals with exaclty the same issue that you have.

Although the code is in VB, I think that you'll get the idea, the solution is pretty straightforward.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜