Solved

How do I access a ListView's datasource programmatically.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

19 Experts available now in Live!

Get 1:1 Help Now