How do I access a ListView's datasource programmatically.

Posted on 2010-08-31
Medium Priority
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

771 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