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
Solved

How do I access a ListView's datasource programmatically.

Posted on 2010-08-31
3
418 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

860 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