DataList (ASP.NET 2.0) set/reset SelectedIndex to -1

J

Jules

We have a problem setting, actually resetting, the SelectedItemTemplate
of a DataList control. Below is the ObjectDataSource and the DataList
in .ASPX:

<asp:ObjectDataSource ID="mySource" runat="server"
SelectMethod="GetStageUsage" TypeName="SomeController">
</asp:ObjectDataSource>
<asp:DataList ID="dl" runat="server"
DataSourceID="mySource" DataKeyField="ID"
OnItemCommand="dl_ItemCommand">
<HeaderTemplate>...</HeaderTemplate>
<ItemTemplate>...</ItemTemplate>
<SelectedItemTemplate>...</SelectedItemTemplate>
</asp:DataList>

The ID column has a LinkButton with CommandName="Select" set. Now, when
we select a certain row, we want to show the SelectedItemTemplate.
Therefore we used the OnItemCommand event. See code below. We used this
code to check if we need to show or hide the SelectedItemTemplate (ie.
show hide details of that item in the grid). When the details for an
item are already shown, and we select it again, the details must hide
(ie. collapse).

protected void dl_ItemCommand(object source, DataListCommandEventArgs
e)
{
DataList fromList = (DataList)source;

if (e.CommandName == "Select")
{
if (fromList.SelectedIndex == e.Item.ItemIndex)
{
fromList.SelectedIndex = -1;
}
else
{
fromList.SelectedIndex = e.Item.ItemIndex;
}

// Rebind the datasource to the datalist
fromList.DataBind();
}
}

In this example we check if the CommandName is "Select" and check
to show or hide the SelectedItemTemplate. Now, when we select an item,
the SelectedItem opens (as expected). When we want to hide/collapse the
details we set the SelectedIndex op -1. However, it does not behave as
it "should". The SelectedIndex is set to -1, but on a PostBack it
has the value again of the e.Item.Index. Therefore, it always goes
setting the value to -1, instead of reopening it again (btw; when we
select a other item it opens because the e.Item.Index has another
value).

Finally we found a workaround, and we're not sure if this is the way
to go. We disabled the ViewState of the DataList (dl) in the .ASPX.
Then we changed the ItemCommand event into the code below. Notice that
we changed the check of the fromList to our own ViewState:

protected void dl_ItemCommand(object source, DataListCommandEventArgs
e)
{
DataList fromList = (DataList)source;

if (e.CommandName == "Select")
{
if (Convert.ToInt32(ViewState["selectedItemIndex"]) ==
e.Item.ItemIndex)
{
fromList.SelectedIndex = -1
ViewState["selectedItemIndex"] = -1;
}
else
{
fromList.SelectedIndex = e.Item.ItemIndex;
ViewState["selectedItemIndex"] = e.Item.ItemIndex;
}

// Rebind the datasource to the datalist
fromList.DataBind();
}

This does do the trick, but we're not sure if this is a good
practice? Any thoughts?

Thanks,

Jules
 
G

guanpedro

Hi Jules,

It all looks correct to me.

Setting the SelectedIndex back to -1 and rebinding the list should be
fine. The event should return the correct item index following the
setting of -1. Does anyone know of issues setting the selectedIndex to
-1 and the SelectedItemIndex in the itemcommand event?

Jonathan

We have a problem setting, actually resetting, the SelectedItemTemplate
of a DataList control. Below is the ObjectDataSource and the DataList
in .ASPX:

<asp:ObjectDataSource ID="mySource" runat="server"
SelectMethod="GetStageUsage" TypeName="SomeController">
</asp:ObjectDataSource>
<asp:DataList ID="dl" runat="server"
DataSourceID="mySource" DataKeyField="ID"
OnItemCommand="dl_ItemCommand">
<HeaderTemplate>...</HeaderTemplate>
<ItemTemplate>...</ItemTemplate>
<SelectedItemTemplate>...</SelectedItemTemplate>
</asp:DataList>

The ID column has a LinkButton with CommandName="Select" set. Now, when
we select a certain row, we want to show the SelectedItemTemplate.
Therefore we used the OnItemCommand event. See code below. We used this
code to check if we need to show or hide the SelectedItemTemplate (ie.
show hide details of that item in the grid). When the details for an
item are already shown, and we select it again, the details must hide
(ie. collapse).

protected void dl_ItemCommand(object source, DataListCommandEventArgs
e)
{
DataList fromList = (DataList)source;

if (e.CommandName == "Select")
{
if (fromList.SelectedIndex == e.Item.ItemIndex)
{
fromList.SelectedIndex = -1;
}
else
{
fromList.SelectedIndex = e.Item.ItemIndex;
}

// Rebind the datasource to the datalist
fromList.DataBind();
}
}

In this example we check if the CommandName is "Select" and check
to show or hide the SelectedItemTemplate. Now, when we select an item,
the SelectedItem opens (as expected). When we want to hide/collapse the
details we set the SelectedIndex op -1. However, it does not behave as
it "should". The SelectedIndex is set to -1, but on a PostBack it
has the value again of the e.Item.Index. Therefore, it always goes
setting the value to -1, instead of reopening it again (btw; when we
select a other item it opens because the e.Item.Index has another
value).

Finally we found a workaround, and we're not sure if this is the way
to go. We disabled the ViewState of the DataList (dl) in the .ASPX.
Then we changed the ItemCommand event into the code below. Notice that
we changed the check of the fromList to our own ViewState:

protected void dl_ItemCommand(object source, DataListCommandEventArgs
e)
{
DataList fromList = (DataList)source;

if (e.CommandName == "Select")
{
if (Convert.ToInt32(ViewState["selectedItemIndex"]) ==
e.Item.ItemIndex)
{
fromList.SelectedIndex = -1
ViewState["selectedItemIndex"] = -1;
}
else
{
fromList.SelectedIndex = e.Item.ItemIndex;
ViewState["selectedItemIndex"] = e.Item.ItemIndex;
}

// Rebind the datasource to the datalist
fromList.DataBind();
}

This does do the trick, but we're not sure if this is a good
practice? Any thoughts?

Thanks,

Jules
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top