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
ataList ID="dl" runat="server"
DataSourceID="mySource" DataKeyField="ID"
OnItemCommand="dl_ItemCommand">
<HeaderTemplate>...</HeaderTemplate>
<ItemTemplate>...</ItemTemplate>
<SelectedItemTemplate>...</SelectedItemTemplate>
</asp
ataList>
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
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
DataSourceID="mySource" DataKeyField="ID"
OnItemCommand="dl_ItemCommand">
<HeaderTemplate>...</HeaderTemplate>
<ItemTemplate>...</ItemTemplate>
<SelectedItemTemplate>...</SelectedItemTemplate>
</asp
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