I have a webgrid where I would like to add a column where each cell contains a name and an ID, with the name displayed in the cell. I can do this by creating my own class as below, and make each cell element an instance of this class:
private class NameAndID { private string name; private int ID; public NameAndID(string name, int ID) { this.name = name; this.ID = ID; } public override string ToString() { return this.name; } public int getID() { return this.ID; } }
This works server-side because I can use the getID method to retrieve the ID of an element, but how do I get the ID at client-side with javascript? The cell.getValue() javascript method only returns the name! And is this the way to go or can I create a custom column that contains a name and an ID in some other way?
EDIT: I have tried making a hidden column that contains the ID but this doesn't work, because a hidden column is not sorted when the grid is sorted, and therefore the proper IDs are not linked to the proper names after a sort, so I would like to avoid hidden columns!
you can do one thing.
make this column as type="dropdown" and then put ID as value and whatever you want to display as DisplayText.
if you do getValue() it will give you ID if you want text you can use this in javascript.
cell.Element.innerText
I hope this gives answer.
Thanks for your response. I would like to have the cells in the column editable to the user, so the user can enter a new, custom name in each cell. This cannot be done if I use a column with drop-down lists (at least I don't know how). Besides that, your solution was fine. Any other ideas?