Scott on Writing

Musings on technical writing...

Setting the DataList's SelectedIndex Programmatically

Have you ever tried to set the SelectedIndex property of a DataList programmatically, so that the SelectedItemTemplate will be used?  For example, you might be binding to the DataList information about products, and you might want to have the cheapest or most expensive product “selected” so that it is rendered via the SelectedItemTemplate.  Initially, you might think to solve this problem by creating an event handler for the DataList's ItemDataBound event, and then to programmatically set the SelectedIndex property based on the index of the row that meets whatever criteria you have outlined to be “selected.” 

Sadly, this approach won't work due to the order of events in rendering a DataList.  Let's step through the series of events that transpire when the DataList's DataBind() method is called. For each item in the DataList's DataSource, the following things happen in this order:

  1. A DataListItem instance is created.
  2. The proper template is used to render the contents of the DataListItem. It's at this point that the DataList's SelectedIndex is checked to see whether the ItemTemplate should be used or if the SelectedItemTemplate should be used.
  3. The DataList's ItemCreated event is raised.
  4. The DataListItem's DataItem property is set to the current DataSource row, and the DataListItem's DataBind() method is called.
  5. The DataList's ItemDataBound event is raised.

By examining the above sequence of events, it hopefully is evident why we can't simply set the SelectedIndex property in the DataList's ItemDataBound event and have it work as planned. The reason is because the SelectedItemTemplate has already been applied several steps before the DataList's ItemDataBound event fires. So, if setting the SelectedIndex in the ItemDataBound event handler is too late, where can we set this property?

The answer is that you have to set it before calling the DataBind() method.  Namely, after getting your data from the database but before calling the DataList's DataBind() method, you have to iterate through the records and determine the record index of the record that is to be selected.  Then, you have to set this property, and only then can you call DataBind().  This approach solves the problem discussed previously because it sets the SelectedIndex property before any of the five steps transpire.

If you want to learn more, check out the in-depth FAQ I wrote on this topic: Setting the SelectedIndex Programmatically.

Happy Programming!

posted on Friday, October 17, 2003 11:09 AM

Feedback

# re: Setting the DataList's SelectedIndex Programmatically 1/22/2006 10:13 PM Alex

This was idiotic back then and it's even more stupid now with 2.0 out. This basically makes the whole approach of DataSources completely invalid.

The more I work with asp.net the more it seems that the MS team is clearly split into two... you have smart folks working on things like Atlas, and then there are completely incompetent retards working on built in asp.net controls...

god damn... i had to find an open source version of the calendar and now i have to find open source version of the datalist too... good thing i only killed 3 hours trying to make the bloody thing work thinking i have to be doing something wrong...

# re: Setting the DataList's SelectedIndex Programmatically 1/23/2006 10:12 AM Scott Mitchell

You have some good points, Alex. And I agree that the Calendar control is one of those built-in ASP.NET server controls that has some 'quirks' as well! :-)

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2008>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 1896.8%
Monday 39014.0%
Tuesday 46916.8%
Wednesday 51518.5%
Thursday 54419.5%
Friday 50818.2%
Saturday 1706.1%
Total 2785100.0%

Hour1Total% of Total
12:00 AM 682.4%
1:00 AM 712.5%
2:00 AM 632.3%
3:00 AM 752.7%
4:00 AM 572.0%
5:00 AM 1093.9%
6:00 AM 1114.0%
7:00 AM 1615.8%
8:00 AM 1756.3%
9:00 AM 1505.4%
10:00 AM 1736.2%
11:00 AM 1826.5%
12:00 PM 1906.8%
1:00 PM 1766.3%
2:00 PM 1605.7%
3:00 PM 1324.7%
4:00 PM 1124.0%
5:00 PM 983.5%
6:00 PM 913.3%
7:00 PM 993.6%
8:00 PM 853.1%
9:00 PM 802.9%
10:00 PM 833.0%
11:00 PM 843.0%
Total 2785100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.35353
Tuesday 4.35444
Wednesday 7.58644
Thursday 6.87625
Friday 5.45414
Saturday 5.03161
Total 5.802785

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.45109
9:00 AM 6.34279
10:00 AM 6.41250
11:00 AM 4.28184
12:00 PM 6.98342
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.54299
4:00 PM 3.9190
5:00 PM 5.78156
6:00 PM 4.52113
7:00 PM 9.32177
8:00 PM 9.06154
9:00 PM 5.14113
10:00 PM 6.2381
11:00 PM 4.5732
Total 5.802785

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles