ASP.NET GridView issue

Well I have a gridview where I have defined the columns myself and disabled auto-generation, but now I have a problem that I cannot access the GridView.SelectedRow.DataItem.

As it turns out, it is now null when it mattered when autogeneration was enabled.

Edit: What I need is a way to keep the string id until showing the id to the user, if there is any way to do this?

+1


a source to share


2 answers


I am assuming the DataItem is only populated when using the DataBinding. Are you using DataBinding?

Well from url :

The GridView (and, in fact, all of our data controls) does not persist these items across postbacks. This reduces the ViewState (if the objects are even serializable) and allows garbage collection and cleanup of objects. So when you click to go back, the GridView has an uncalled DataBind and so your data is not there. This is what you found.



Assuming you are reading the value from the postback, there might be a problem.

Try using SelectedValue if you have customized the (primary) key for the items. I've always used this and it worked. msdn about SelectedValue

+5


a source


You can create a new hidden template column that will have a label with an ID. and in the cs file you are using .FindControl for strings.



You also have a DataKeys property on the gridview, because I also think you want

+1


a source







All Articles