Solved

asp DataList SelectedItemTemplate out of step

Posted on 2011-09-17
2
244 Views
Last Modified: 2012-08-14
I have a datalist listing a number of records (record1, 2, 3 etc). There is an ItemTemplate and a SelectedItemTemplate, each identified by a literal value, and a field from the data.

I want to click the Linkbutton and display the SelectedItemTemplate for the selected record. However, the display of the SelectedItemTemplate is out of step by one: if I click the linkbutton for record2, nothing changes. If I click the linkbutton for record4, the SelectedItemTemplate for record 2 is displayed. If I click the linkbutton for record8, the SelectedItemTemplate for record 4 is displayed. The  SelectedItemTemplate is always one behind the record I last selected.

Code is attached. Am I doing something silly or stupid?
<%@ Page Language="VB" MasterPageFile="~/Partner/PartnerMasterPage2.master" Title="Tenant Assistance Program" StylesheetTheme="Theme1" %>
<script runat="server">
    
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="contentPlaceHolder1" Runat="Server">

    <asp:Panel ID="pnlMulti" runat="server">
        <asp:DataList ID="lstTenants" runat="server" DataKeyField="Tenant_ID" 
        DataSourceID="SqlDataTenantNames" Width="485px" >

            <ItemTemplate>
                ITEM Template:<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" 
                    CssClass="lblBuildingTitle2" Text='<%# Eval("Tenant_Name") %>'></asp:LinkButton>
                <br />
            </ItemTemplate>

            <SelectedItemTemplate>
                SELECTED Template:<asp:Label ID="Tenant_NameLabel" runat="server" CssClass="lblBuildingTitle2" 
                    Text='<%# Eval("Tenant_Name") %>' />
                    <br />
            </SelectedItemTemplate>

        </asp:DataList>
    </asp:Panel>

    <asp:SqlDataSource ID="SqlDataTenantNames" runat="server" 
        ConnectionString="<%$ ConnectionStrings:dbCOMPASSConnectionString %>" 
        SelectCommand="SELECT Building_ID, Tenant_ID, Tenant_Name FROM tblPartnerBuildingTenants WHERE (Building_ID = @Building_ID) ORDER BY Sort_Seq, Tenant_Name">
        <SelectParameters>
            <asp:SessionParameter Name="Building_ID" SessionField="Building_ID" />
        </SelectParameters>
    </asp:SqlDataSource>
</asp:Content>

Open in new window

0
Comment
Question by:slimguy
2 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 250 total points
ID: 36558296
In the Item_Command set the Selectedindex of it as ItemsList.SelectedIndex = e.Item.ItemIndex;

Refer the link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.selecteditemtemplate.aspx

0
 

Author Closing Comment

by:slimguy
ID: 36558736
Thanks, it wasn't obvious that I needed to manually set the SelectedIndex value. The answer didn't work exactly as suggested in the msdn sample - I had another datalist and datasource in the SelectedItemTemplate that errored with a missing container. Putting the  DataBind into the Page_PreRender instead fixed it (possibly not elegant, but worked).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question