Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 15th, 2004, 02:54 PM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting Datalist before Databinding

I am grappling with the following

- I wish to select an item from a DataList based on an attribute of a DataListItem in the List

As far as I know this must be done before I do myDataList.DataBind()
but I'm using the OnItemDataBound method to determine whether I should select a particular item, and this gets fired after the databinding... so the item doesn't gets selected until the following postback.

If I try to loop through the items of the DataList before databinding looking for the one to select, they have not been created yet, and also I cannot Databind twice, as the first call closes my DataReader.

Here is some of the code:

private void Page_Load(object sender, System.EventArgs e){

  // Setting selected index really needs to be done here

  if(!IsPostBack){
    CategoryList.DataSource = ProductsDB.GetProductCategories();
    CategoryList.DataBind();
  }
}


protected void CategoryList_ItemDataBound(Object sender, DataListItemEventArgs e){
  string paramCategoryID = Request.Params["CategoryID"];

  // Check that the DataItem has been created, and the http Parameter exists
  if(paramCategoryID != null && e.Item.DataItem != null)

    // Check if the current DataItem is the one we want to select
    if(((IDataRecord)e.Item.DataItem)["CategoryID"].ToString()==paramCategoryID )
      CategoryList.SelectedIndex = e.Item.ItemIndex;

}

  #2 (permalink)  
Old November 15th, 2004, 04:41 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should be able to find the item index you wish to select, then set the datalist SelectedItemIndex value. Then you DataBind(). Now when the individual items are bound, the item at the index you set will get created and bound as a selected item.
  #3 (permalink)  
Old November 16th, 2004, 11:50 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Below is the code which tries to set the selected index before databinding. Unfortunately my conundrum remains; that searching for a particular value in the datalist fails as the items of the datalist are not populated until databinding.

Is this to do with the fact that a datareader is a forward only type thing which just outputs one row at a time?


string categoryID = Request.Params["CategoryID"];

// Step 1, get the datasource
CategoryList.DataSource = ProductsDB.GetProductCategories();

// Step 2, try to find the particular dataitem
// in order to select it
int i = 0;

if(categoryID != null)

    // the following fails as CategoryList.Items.Count == 0
    foreach(DataListItem item in CategoryList.Items){

        if(item.DataItem != null)
            if(((IDataRecord)item.DataItem)["CategoryID"].ToString()==categoryID){

                CategoryList.SelectedIndex = i;
                break;

            }
        i++;

    }

// Step 3. Databind
CategoryList.DataBind();



  #4 (permalink)  
Old November 16th, 2004, 03:32 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

That sounds like it would be a problem. You'll probably need to change the DataReader to a DataTable so that you can look thru it to find the item you wish to select.
  #5 (permalink)  
Old November 17th, 2004, 09:24 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Planoie,

I changed my DataSource from a DataReader to a lowly ArrayList, (specifically ProductsDB.GetProductCategories() now does extra processing to populate and return an ArrayList) and was able to loop through the list easily... This is not a problem seeing as my list is not too large.

Eoghan



Similar Threads
Thread Thread Starter Forum Replies Last Post
DataBinding with Radiobuttons vgsgowrisankar C# 2005 0 May 8th, 2008 05:15 AM
Datalist inside datalist amit.jagtap ASP.NET 2.0 Professional 1 September 4th, 2007 05:03 AM
DataList DataBinding Error: IndexOutOfRange AnotherAspdotnetDeveloper ASP.NET 2.0 Professional 0 February 20th, 2007 05:51 AM
databinding in VB2005 Bhuvana ADO.NET 0 March 9th, 2006 02:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.