473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3140
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Barry | last post by:
Hi, I have a Datalist inside the ItemTemplate of another DataList. When I drilldown, I'm not getting any data in either the itemtemplate or headertemplate of the second DataList. I've confirmed...
2
by: David D. McCrory | last post by:
This is the second post for this question....please help!! This is a fairly simple request....I am looking for some help.... I want to set the SelectedIndex of a DataList control to equal a...
4
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
1
by: mahsa | last post by:
H data I'm binding to the DataList is information about products in my product database. Perhaps passed in the QueryString to this page is a ProductID. I might want to make the item in the DataList...
3
by: CVerma | last post by:
Hi, I have an embedded datagrid within a datalist. I am not able to perfrom paging in the datagrid. Any ideas? Here is my code: Here is my Simplegrid.cs file: using System; using...
0
by: rbutch | last post by:
i should have included the code in the first post. sorry. i've got this working but it returns the 'index' property whereas i'd like the value of what's in the property.i'll include the minimalist...
1
by: Neil Jarman | last post by:
Hi, I'm new to this today, and I've got some test code (see below.) The data loads fine. I can't understand why any of the events fire. Once the page loads, clicking on thew button from the...
0
by: Isz | last post by:
PROBLEM: This problem is related to the postback and handling events correctly. BACKGROUND: I have a datalist which acts as a tabbes list with horizontal layout. This datalist is bound to a...
0
by: Les Caudle | last post by:
I have a menu system composed of a DataList nested inside a DataList. The outer DataList has it's DataSource (composed of a DataSet with two tables linked by a CategoryPagesRelation Relation) set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.