473,769 Members | 3,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

We have a problem setting, actually resetting, the SelectedItemTem plate
of a DataList control. Below is the ObjectDataSourc e and the DataList
in .ASPX:

<asp:ObjectData Source ID="mySource" runat="server"
SelectMethod="G etStageUsage" TypeName="SomeC ontroller">
</asp:ObjectDataS ource>
<asp:DataList ID="dl" runat="server"
DataSourceID="m ySource" DataKeyField="I D"
OnItemCommand=" dl_ItemCommand" >
<HeaderTemplate >...</HeaderTemplate>
<ItemTemplate>. ..</ItemTemplate>
<SelectedItemTe mplate>...</SelectedItemTem plate>
</asp:DataList>

The ID column has a LinkButton with CommandName="Se lect" set. Now, when
we select a certain row, we want to show the SelectedItemTem plate.
Therefore we used the OnItemCommand event. See code below. We used this
code to check if we need to show or hide the SelectedItemTem plate (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, DataListCommand EventArgs
e)
{
DataList fromList = (DataList)sourc e;

if (e.CommandName == "Select")
{
if (fromList.Selec tedIndex == e.Item.ItemInde x)
{
fromList.Select edIndex = -1;
}
else
{
fromList.Select edIndex = e.Item.ItemInde x;
}

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

In this example we check if the CommandName is "Select" and check
to show or hide the SelectedItemTem plate. 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, DataListCommand EventArgs
e)
{
DataList fromList = (DataList)sourc e;

if (e.CommandName == "Select")
{
if (Convert.ToInt3 2(ViewState["selectedItemIn dex"]) ==
e.Item.ItemInde x)
{
fromList.Select edIndex = -1
ViewState["selectedItemIn dex"] = -1;
}
else
{
fromList.Select edIndex = e.Item.ItemInde x;
ViewState["selectedItemIn dex"] = e.Item.ItemInde x;
}

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

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 3161
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 SelectedItemInd ex in the itemcommand event?

Jonathan
Jules wrote:
We have a problem setting, actually resetting, the SelectedItemTem plate
of a DataList control. Below is the ObjectDataSourc e and the DataList
in .ASPX:

<asp:ObjectData Source ID="mySource" runat="server"
SelectMethod="G etStageUsage" TypeName="SomeC ontroller">
</asp:ObjectDataS ource>
<asp:DataList ID="dl" runat="server"
DataSourceID="m ySource" DataKeyField="I D"
OnItemCommand=" dl_ItemCommand" >
<HeaderTemplate >...</HeaderTemplate>
<ItemTemplate>. ..</ItemTemplate>
<SelectedItemTe mplate>...</SelectedItemTem plate>
</asp:DataList>

The ID column has a LinkButton with CommandName="Se lect" set. Now, when
we select a certain row, we want to show the SelectedItemTem plate.
Therefore we used the OnItemCommand event. See code below. We used this
code to check if we need to show or hide the SelectedItemTem plate (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, DataListCommand EventArgs
e)
{
DataList fromList = (DataList)sourc e;

if (e.CommandName == "Select")
{
if (fromList.Selec tedIndex == e.Item.ItemInde x)
{
fromList.Select edIndex = -1;
}
else
{
fromList.Select edIndex = e.Item.ItemInde x;
}

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

In this example we check if the CommandName is "Select" and check
to show or hide the SelectedItemTem plate. 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, DataListCommand EventArgs
e)
{
DataList fromList = (DataList)sourc e;

if (e.CommandName == "Select")
{
if (Convert.ToInt3 2(ViewState["selectedItemIn dex"]) ==
e.Item.ItemInde x)
{
fromList.Select edIndex = -1
ViewState["selectedItemIn dex"] = -1;
}
else
{
fromList.Select edIndex = e.Item.ItemInde x;
ViewState["selectedItemIn dex"] = e.Item.ItemInde x;
}

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

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
4949
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 that the datasource is being populated properly, however the itemdatabound event is not being fired. Here's some code: <asp:datalist id="BatchList" runat="server" repeatdirection="Vertical" headerstyle-cssclass="CategoryHeader"...
2
3099
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 specific record in the database. What is the easiest way to accomplish this?? Thanks, David D. McCrory
4
3096
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 label, a radiobuttonlist, etc. I'm driving myself insane trying to figure out how to get
1
1671
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 that has the corresponding ProductID selected. In a similar vein, perhaps I want to have newest product selected by default. How can I acheieve this functionality actuly i use pagin <%@ Page language="c#" Codebehind="product.aspx.cs"...
3
2782
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 System.Collections; using System.ComponentModel; using System.Data;
0
921
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 of code here <asp:datalist id="Datalist1" OnItemCommand="getSelected"> <SelectedItemTemplate> <asp:HyperLink ID="ot_job_id" Runat="server" /> </SelectedItemTemplate>
1
2152
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 ItemTemplate does not fire the OnItemCommand event. It just fires a page reload - as a postback. I guess I'm missing something really obvious and important here?
0
3948
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 strogly typed collection I called TabCollection which is a collevtion of items called tab. Each tab is as follows:
0
1649
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 in the Page_Load. The inner DataList has its DataSource set in the ascx file as: <asp:DataList ID="PageList" runat="server" CellPadding="3" CellSpacing="0" DataSource='<%#...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9851
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.