View Single Post
  #24 (permalink)  
Old April 14th, 2010, 11:44 AM
martinspalding martinspalding is offline
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default ListView ItemDataBound

Imar,

Sorry for my late reply!

I finally got your rating control to work within my asp.net listview control based on your repeater example. Please find my pages attached...

*************DEFAULT.ASPX*************

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/SingleColumn.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Spaanjaars.Toolkit" Namespace="Spaanjaars.Toolkit" TagPrefix="isp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <div>
            <asp:Updatepanel ID="TrustAccountsUpdatePanel" runat="server" UpdateMode="Conditional">
                <contentTemplate>  
                    <div id="dlg" class="panelTrustAccounts" style="width:915px">
                        <div class="bodyTrustAccounts">
                            <div class="outerTrustAccounts">
                                <div class="innerTrustAccounts">
                                    <div class="contentTrustAccounts">
                                        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                                        <asp:ObjectDataSource 
			                                ID="ObjectDataSource1" 
			                                runat="server" 
			                                SelectMethod="GetList" 
			                                TypeName="Traders">
			                            </asp:ObjectDataSource>
                                        <asp:ListView 
                                            ID="lvTrustAccounts" 
                                            runat="server"
                                            DataSourceID="ObjectDataSource1"
                                            DataKeyNames="Id"
                                            DataMember="DefaultView" 
                                            OnItemCreated="lvTrustAccounts_ItemCreated"
                                            OnItemDataBound="lvTrustAccounts_ItemDataBound">
                                            <LayoutTemplate>
                                                <table id="TrustAccounts" runat="server" class="gridTrustAccounts" cellspacing="0" border="0">
                                                    <tr>
                                                        <th><asp:LinkButton ID="btnSortTraderRating" runat="server" Text="Overall Rating" CommandName="Sort" CommandArgument="trader_overall_rating" Width="150px" /></th>
                                                    </tr>
                                                    <tr id="itemPlaceholder" runat="server" />
                                                </table>
                                            </LayoutTemplate>
                                            <EmptyDataTemplate>
                                                <table id="Table1" runat="server" style="">
                                                    <tr>
                                                        <td>No data was returned.</td>
                                                    </tr>
                                                </table>
                                            </EmptyDataTemplate>
                                            <ItemTemplate>
                                                <tr class='<%# (Container.DataItemIndex % 2 == 0)?"row":"altrow" %>' id="row" runat="server" style="height:108px;"> 
                                                    <td>
                                                        Book:
					                                    <asp:Label 
					                                        ID="Label1" 
					                                        runat="server" 
					                                        Text='<%# Eval("Name") %>'>
					                                    </asp:Label>
					                                    <isp:ContentRating 
					                                        ID="Rating1" 
					                                        runat="server" 
					                                        OnRated="ContentRating1_Rating" 
					                                        OnRating="ContentRating1_Rating" />
					                                    <br />
                                                    </td>
                                                 </tr>
                                            </ItemTemplate>
                                        </asp:ListView>
                                    </div>
                                </div>
                            </div>
                        </div>                
                    </div>
                </contentTemplate>
            </asp:Updatepanel> 
        </div>
</asp:Content>
*************DEFAULT.CS***************

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using AjaxControlToolkit;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq.Expressions;
using System.Reflection;
using System.Data.SqlClient;
using System.Globalization;
using Spaanjaars.Toolkit;

public partial class _Default : System.Web.UI.Page
{
    public string thisConnectionString = ConfigurationManager.ConnectionStrings["TradeSelectorConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            FormsAuthentication.SetAuthCookie(HttpContext.Current.User.Identity.Name, false);
            Response.Redirect("Customers.aspx");
        }
    }

    protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)   
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        
        //if (e.Item.ItemType == ListViewItemType.DataItem)
        if (e.Item.ItemType == ListViewItemType.DataItem)   
        {   
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;   
            if (myRating != null)   
            {
               Traders myTraders = dataItem.DataItem as Traders;
               if (myTraders != null)
               {
                   myRating.ItemId = myTraders.Id;
                   myRating.DataSource = myTraders.Rating;
               }
            }   
        }   
    }

    protected void lvTrustAccounts_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;
            if (myRating != null)
            {
                Traders myTraders = dataItem.DataItem as Traders;
                if (myTraders != null)
                {
                    myRating.ItemId = myTraders.Id;
                    myRating.DataSource = myTraders.Rating;
                }
            }
        }
    }

    protected void ContentRating1_Rating(object sender, RateEventArgs e)
    {
        ContentRating myRating = sender as ContentRating;
        Label2.Text = string.Format("ItemId: {0} Value {1}", myRating.ItemId, e.RateValue);
    }
}
**********LIST.CS****************

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Traders
{
    public string Name { get; set; }
    public int[] Rating { get; set; }
    public int Id { get; set; }

    public List<Traders> GetList()
    {
        List<Traders> tempList = new List<Traders>();
        for (int i = 0; i < 5; i++)
        {
            Traders myTraders = new Traders();
            myTraders.Id = i;
            myTraders.Name = "Traders " + i.ToString();
            myTraders.Rating = new int[] { 0, 0, 0, 0, 0 };
            myTraders.Rating[i] = i + 1;
            tempList.Add(myTraders);
        }
        return tempList;
    }
}