I'm writting a simple prototype front end using a GridView that is populated via function, rather than being linked directly to a SqlServer data source.
So I can delete a row/record from grid/underlying database I am currently
- Setting the AutoGenerateDeleteButton = true
- Displaying the unique record ids in the first column
- Handling the RowDeleting event
- Obtaining the id by getting the grid.Rows[e.RowIndex].Cells[idIndex].Text
- Passing that number through to a function that does the deleting
This seems to be working just fine, but I would rather not display the ids to the users at they don't mean anything to them.
I tried setting the id column's Visible property to false, but this caused step 4 above to return an empty string - and so no record deleted.
So how do I store the hidded id number with each row?
Or am I going about this completely the wrong way?
Follow up to answers:
Thanks for both the answers, ended up going Eric's DataKeyNames way. For other people new to ASP.NET like I am, the steps I used where
Between the lines where I set the grids DataSource and called DataBind(), I added
grid.DataKeyNames = new string[] {"id"};
Then in the function handling the RowDeleting I got hold of my id using
grid.DataKeys[e.RowIndex].Value
-
GridView has a DataKeyNames property. When you bind a data source to the grid, you set the DataKeyNames (usually with just one name, your PK field). You don't show the PK, but you can get to it from code-behind.
Kon : Yep, DataKeys is the way to go.From Eric Z Beard -
Visible=false means don't render on the page. What you want is either to make it a template field and use a HiddenField to hold the value or set the style on the control to "display: none;". This would be the case if the client side code needed access to the value for an Ajax call or something.
Otherwise use the DataKeyNames property as @Eric Z Beard suggests.
From tvanfosson
0 comments:
Post a Comment