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!
I'm generating it using "Cell and Row", as you said. You're right that I could set the ID as the key like this:
UltraGridCell c = new UltraGridCell();c.Key = ID.ToString();c.Value = Name; row.Cells.Add(c);
but as I see it, I'm not able to get the key from the cell object at client-side according to this page:
http://help.infragistics.com/Help/NetAdvantage/NET/2008.2/CLR2.0/html/WebGrid_cell_Object_CSOM.html
Are you sure the key is accessible?
are you binding your grid with any source or you are generating it using Cell and Row?
if you are generating it you can give your ID as your Cell Key and Text as whatever you want to show.
on client side you can get that key from the cell object.
I am not sure which method or property you can use to get the key but I think it will be accessible at client side.
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?
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.