Solved

asp DataList SelectedItemTemplate out of step

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

24 Experts available now in Live!

Get 1:1 Help Now