468,257 Members | 1,354 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

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

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

Jul 11 '06 #1
1 2940
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
Jules wrote:
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
Jul 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Barry | last post: by
2 posts views Thread by David D. McCrory | last post: by
4 posts views Thread by V. Jenks | last post: by
3 posts views Thread by CVerma | last post: by
reply views Thread by rbutch | last post: by
1 post views Thread by Neil Jarman | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.