Set DataList SelectedIndex - 2nd Post

  • Thread starter Thread starter David D. McCrory
  • Start date Start date
D

David D. McCrory

This is the second post for this question....please help!!

This is a fairly simple request....I am looking for some help....

I want to set the SelectedIndex of a DataList control to equal a specific
record in the database. What is the easiest way to accomplish this??


Thanks,
David D. McCrory
 
David said:
This is the second post for this question....please help!!

This is a fairly simple request....I am looking for some help....

I want to set the SelectedIndex of a DataList control to equal a specific
record in the database. What is the easiest way to accomplish this??


Thanks,
David D. McCrory

I would add a handler for the ItemDataBound event of the DataList and
then the event args give you access to the current item and its data.

e.Item.DataItem gives the current item in the datasource; use this to
check for a certain value
e.Item.ItemIndex gives the current item's index

I believe you can set the selectedindex in this event; if not, set a
local/member var to the value and then after the DataBind, set it.

The other solution is to loop the items after the DataBind instead of
using the event, but doing the first way does one less loop.
 
Thanks Craig...I will give it a try......

Craig Deelsnyder said:
I would add a handler for the ItemDataBound event of the DataList and
then the event args give you access to the current item and its data.

e.Item.DataItem gives the current item in the datasource; use this to
check for a certain value
e.Item.ItemIndex gives the current item's index

I believe you can set the selectedindex in this event; if not, set a
local/member var to the value and then after the DataBind, set it.

The other solution is to loop the items after the DataBind instead of
using the event, but doing the first way does one less loop.
 
Back
Top