How do I access a ListView's datasource programmatically.

Posted on 2010-08-31
Last Modified: 2012-08-14
I have a seriers of Listviews that are embedded within an Accordion control.  These ListViews represent days in a football tournament.  Because of the number of days (or rathat the number of ListViews) I have used two separate .ascx template files for the ListView's LayoutTemplate and ItemTemplate.

Everything is working as it should, except now I need to display 3 separate images depending on the ticket availability - a green image for VIP and Grandstand tickets, amber for Grandstand only, and red for no tickets available.

I have 2 boolean fields in the Match table, VIP and Grandstand to represent the availability.

How can I do this?  I basically need to check what the availability status is, and display the appropriate image.  

Is there a way in MatchItemTemplate.ascx.cs I can check to see what that status is?  In the .ascx I can use <%# Eval("VIP")%>, but how do I use that in the code-behind in an if statement?  It's hard to explain but you should see what I mean by looking at the code snippet.

// *** LISTVIEW ***
//Code-behind for the creation of the ListView
ListView lv1 = new ListView();
lv1.ID = "matchday1FirstListView";
//Set the templates.
lv1.LayoutTemplate = LoadTemplate("Templates/MatchLayoutTemplate.ascx");
lv1.ItemTemplate = LoadTemplate("Templates/MatchItemTemplate.ascx");

//Method to retrive ListView datasource
private IQueryable GetListViewData(string stageName, string matchDayTitle, DateTime date)
	//Get the matches for this stage, match day and date.
        IQueryable matches = TournamentBLL.GetMatches(stageName, matchDayTitle, date);

        //Return the IQueryable Match object.
        return matches;

//*** TEMPLATES ***

//Layout Template: MatchLayoutTemplate.ascx.cs
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MatchLayoutTemplate.ascx.cs"
    Inherits="Custom_Modules_Tournament_Frontend_Templates_MatchLayoutTemplate" %>
    <table cellspacing="0" cellpadding="0" border="0">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>

//ItemTemplate: MatchItemTemplate.ascx.cs
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MatchItemTemplate.ascx.cs"
    Inherits="Custom_Modules_Tournament_Frontend_Templates_MatchItemTemplate" %>
    <td class="flag">
        <img src="<%# Eval("TeamAFlagIconURL")%>" alt="Team Flag" />
    <td class="leftTeam">
        <%# Eval("TeamAName")%>
    <td class="time">
        <%# Eval("Time")%>
    <td class="rightTeam">
        <%# Eval("TeamBName")%>
    <td class="flag">
        <img src="<%# Eval("TeamBFlagIconURL")%>" alt="Team Flag" />
    <td class="availability">
        <img src="<%# Eval("TeamBFlagIconURL")%>" alt="Team Flag" />

//*** BLL ***
public static IQueryable GetMatches(string stageName, string matchDayTitle, DateTime date)
            //Create a new instance of the TournamentDataContext object.
            TournamentDataContext db = new TournamentDataContext();
            //Get all matches
            IQueryable matches = from m in db.Matches
                                 where (m.Date >= date && m.Date <= date.AddHours(24)) &&
                                 m.Stage.Name == stageName &&
                                 m.MatchDay.Title == matchDayTitle
                                 orderby m.Date
                                 select new
                                     MatchDay = m.MatchDay.Title,
                                     Time = string.Format("{0:t}", m.Date),
                                     TeamAFlagIconUrl = m.Team.FlagIconURL,
                                     TeamAName = m.Team.Name,
                                     TeamBFlagIconUrl = m.Team1.FlagIconURL,
                                     TeamBName = m.Team1.Name,                                     
                                     VIP = m.VIP,
                                     Grandstand = m.Grandstand
            return matches;

Open in new window

Question by:markerasmus
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

Accepted Solution

zapacila89 earned 500 total points
ID: 33572949
access ListView1.Items

more weird way:

if you can use it in ascx and display base on <%# Eval("VIP")%> test why don't you set something like a title to the current div (in ascx) and in code behind test that div's title ?

Author Comment

ID: 33586248
Zapacilia89.  Your suggestion got me my solution.  Instead of using 2 fields in the DB I used one of DBType int, with possible values of 0,1,2,3 - not available, VIP available, Grandstand available, VIP and Grandstand available.  The I used a hidden input field and assigned the value <%# Eval("available")%>, and then put the logic in the code behind to test for availability.


Expert Comment

ID: 33595523
Glad i could help!

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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