Solved

asp DataList SelectedItemTemplate out of step

Posted on 2011-09-17
2
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Iteration Help (Asp.net VB) 5 37
Finding the IIS version 5 39
asp.net open new page without popup blocker 8 41
How to filter by key press ? 6 57
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

697 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