Solved

asp DataList SelectedItemTemplate out of step

Posted on 2011-09-17
2
242 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now