|
Subject:
|
Selecting Datalist before Databinding
|
|
Posted By:
|
eoghan
|
Post Date:
|
11/15/2004 1:54:53 PM
|
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;
}
|
|
Reply By:
|
planoie
|
Reply Date:
|
11/15/2004 3:41:00 PM
|
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.
|
|
Reply By:
|
eoghan
|
Reply Date:
|
11/16/2004 10:50:24 AM
|
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();
|
|
Reply By:
|
planoie
|
Reply Date:
|
11/16/2004 2:32:16 PM
|
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.
|
|
Reply By:
|
eoghan
|
Reply Date:
|
11/17/2004 8:24:42 AM
|
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
|