473,386 Members | 1,773 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,386 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 3138
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.