Link to home
Start Free TrialLog in
Avatar of slimguy
slimguy

asked on

asp DataList SelectedItemTemplate out of step

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

ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slimguy
slimguy

ASKER

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).