Solved

How do I access a ListView's datasource programmatically.

Posted on 2010-08-31
3
406 Views
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.

Thanks.
// *** 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" %>
<center>
    <table cellspacing="0" cellpadding="0" border="0">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
    </table>
</center>


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


//*** 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

0
Comment
Question by:markerasmus
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
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 ?
0
 

Author Comment

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

Thanks.
0
 
LVL 1

Expert Comment

by:zapacila89
ID: 33595523
Glad i could help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

15 Experts available now in Live!

Get 1:1 Help Now