468,247 Members | 1,434 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

DataList DataKeys

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?

Oct 10 '06 #1
2 2960
q
Does your code look like this?

private List<Tlist;

public List<TList {
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<TList {
get {return (List<T>)ViewState["ListT"];}
set {ViewState["ListT"]=value;}
}

.....

cmay wrote:
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?
Oct 10 '06 #2
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.

q wrote:
Does your code look like this?

private List<Tlist;

public List<TList {
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<TList {
get {return (List<T>)ViewState["ListT"];}
set {ViewState["ListT"]=value;}
}

....

cmay wrote:
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?
Oct 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by vjmehra | last post: by
4 posts views Thread by Marina | last post: by
4 posts views Thread by jenn | last post: by
2 posts views Thread by Bruno Piovan | last post: by
7 posts views Thread by Andy Sutorius | last post: by
1 post views Thread by Adam Knight | last post: by
1 post views Thread by MikeB | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.