DataList DataKeys

  • Thread starter Thread starter cmay
  • Start date Start date
C

cmay

I am binding a List(Of T) to a DataList.

In this case, T is a class Test, which has a property TestId.

I am trying to use the DataKeyField with this
<asp:DataList DataKeyField="TestId" ...

The datalist displays fine, but when I do a post back, the DataKeys
collection is empty.

Is it not possible to use DataKeys w/ a List(Of T)? Could there be
anything I am doing wrong?
 
Does your code look like this?

private List<T> list;

public List<T> List {
get {return list;}
set {list=value;}
}

void Page_Load(object sender, EventArgs ea) {
DataList.DataSource = this.List;
DataList.DataBind( );
}

If so... you have two problems...

1) You need to have the DataBind( ); like this...

if(!Page.IsPostBack) {
DataList.DataSource = this.List;
DataList.DataBind( );
}


And secondly... depending on what you're doing you may lose your data
in the List...

try this...


public List<T> List {
get {return (List<T>)ViewState["ListT"];}
set {ViewState["ListT"]=value;}
}

.....
 
I'm not sure what happened...
I tried writing my code to use the viewstate, and things seemed to
work, but then when I backed out the changes I made, everything kept
working, until I was back to my original code, and it works now.

The only thing I can guess is that somehow my changes weren't getting
applied, or built correctly.

Thanks for the response.




Does your code look like this?

private List<T> list;

public List<T> List {
get {return list;}
set {list=value;}
}

void Page_Load(object sender, EventArgs ea) {
DataList.DataSource = this.List;
DataList.DataBind( );
}

If so... you have two problems...

1) You need to have the DataBind( ); like this...

if(!Page.IsPostBack) {
DataList.DataSource = this.List;
DataList.DataBind( );
}


And secondly... depending on what you're doing you may lose your data
in the List...

try this...


public List<T> List {
get {return (List<T>)ViewState["ListT"];}
set {ViewState["ListT"]=value;}
}

....


I am binding a List(Of T) to a DataList.

In this case, T is a class Test, which has a property TestId.

I am trying to use the DataKeyField with this
<asp:DataList DataKeyField="TestId" ...

The datalist displays fine, but when I do a post back, the DataKeys
collection is empty.

Is it not possible to use DataKeys w/ a List(Of T)? Could there be
anything I am doing wrong?
 
Back
Top