 |
| ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 7th, 2010, 12:10 PM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm trying to build my listview's ItemDataBound event from the same event from your repeater example but I get the below error...?
protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) (ERROR: operator '||' and '==' cannot be applied to operands of type 'System.Web.UI.WebControls.ListViewItemType' and ''System.Web.UI.WebControls.ListItemType')
{
ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;
if (myRating != null)
{
Book myBook = e.Item.DataItem as Book;
if (myBook != null)
{
myRating.ItemId = myBook.Id;
myRating.DataSource = myBook.Rating;
}
}
}
}
|
|

April 7th, 2010, 12:27 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Just let IntelliSense help you when you type:
e.Item.ItemType ==
You'll get a list with all the different options available for the item type.
Alternatively, look in the documentation: http://msdn.microsoft.com/en-us/libr....itemtype.aspx
Cheers,
Imar
|
|

April 14th, 2010, 11:44 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
}
|
|

April 14th, 2010, 11:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Excellent. Thanks for the update.
Imar
|
|

April 14th, 2010, 11:54 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How do I now replace the above list.cs datasource with my original linqdatasource which provides my listview (thus your rating control) with the appropriate UserId values of all my listview items? Previously I was getting an error when trying to map your rating controls "ItemId" attribute to my "UserId" guid value.
Thanks.
|
|

April 14th, 2010, 12:14 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You should be able to just replace it.
Last edited by Imar; April 14th, 2010 at 01:34 PM..
|
|

April 15th, 2010, 05:41 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Linq Query
I'm not too sure if I understand how to do this replace. Below is my linq query if you could assist? My listview ID is "lvTrustAccounts"...
Code:
TradeSelectorDataContext db = new TradeSelectorDataContext();
var query =
(
from tt in db.tblTraders
join ttcl in db.tblCityLookups on tt.city_id equals ttcl.city_id
join lctt in db.tblCountryLookups on tt.country_id equals lctt.country_id
join tcl in db.tblCountyLookups on tt.county_id equals tcl.county_id
join twal in db.tblTraderWorkingAreaLookups on tt.trader_working_area_id equals twal.trader_working_area_id
join ttrade in db.tblTraderTrades on tt.UserId equals ttrade.UserId
join trade in db.tblTrades on ttrade.trade_id equals trade.trade_id
join ttt in db.tblTradeTradeTypes on trade.trade_id equals ttt.trade_id
join tradet in db.tblTradeTypes on ttrade.trade_type_id equals tradet.trade_type_id
join cj in db.tblCustomerJobs on trade.trade_id equals cj.trade_id
join tr in db.tblTraderRatings on tt.UserId equals tr.UserId
//join tc in db.tblCustomers on cj.UserId equals tc.UserId
orderby tt.trader_firstname ascending
select new
{
tt.UserId
,
tt.trader_title
,
tt.trader_firstname
,
tt.trader_surname
,
tt.trader_phone1
,
tt.trader_phone2
,
tt.trader_working_area_id
,
tt.trader_overall_rating
,
trader_working_area_name = twal.trader_working_area_name.Substring(0, twal.trader_working_area_name.IndexOf('(')).Trim()
,
trader_working_area_postcodes = twal.trader_working_area_name.Substring(twal.trader_working_area_name.IndexOf('(') + 1, twal.trader_working_area_name.LastIndexOf(')') - twal.trader_working_area_name.IndexOf('(') - 1).Trim()
,
trade_type_name = tradet.trade_type_name == null ? "" : tradet.trade_type_name
,
trade.trade_name
,
ttrade.trade_type_id
,
ttrade.trade_id
,
tr.Rating01
,
tr.Rating02
,
tr.Rating03
,
tr.Rating04
,
tr.Rating05
}
).Distinct();
lvTrustAccounts.DataSource = query;
lvTrustAccounts.DataBind();
Thanks.
|
|

April 15th, 2010, 06:03 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
If that query returns objects that look like your test class, this should work.
What is not working? Do you get an error?
Imar
|
|

April 15th, 2010, 06:58 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Error
The error I receive is the one I received originally: "Can't bind a datasource without a valid ItemId"?
What I've done is I've altered my ItemDataBound event to accomodate for each list items rating values...
Code:
protected void lvTrustAccounts_ItemDataBound(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)
{
Object dataRecord = ((ListViewDataItem)e.Item).DataItem; // Get the dataitem and place it into dataRecord
Type anonType = dataRecord.GetType(); // get the anonymous type
// Get user id and rating values using reflection
Guid userid = (Guid)anonType.GetProperty("UserId").GetValue(dataRecord, null);
int rating1 = (int)anonType.GetProperty("Rating01").GetValue(dataRecord, null);
int rating2 = (int)anonType.GetProperty("Rating02").GetValue(dataRecord, null);
int rating3 = (int)anonType.GetProperty("Rating03").GetValue(dataRecord, null);
int rating4 = (int)anonType.GetProperty("Rating04").GetValue(dataRecord, null);
int rating5 = (int)anonType.GetProperty("Rating05").GetValue(dataRecord, null);
// Put rating values in array
int[] rateValues = new int[]
{
rating1, rating2, rating3, rating4, rating5
};
myRating.ItemId = myTraders.Id;
myRating.DataSource = myTraders.Rating;
}
}
}
}
and instead of using the ObjectDataSource to populate my listview I am using the below LinqDataSource code...
Code:
<asp:LinqDataSource
ID="TrustsDataSource"
runat="server"
ContextTypeName="TradeSelectorDataContext"
EnableDelete="True"
EnableInsert="True"
EnableUpdate="True"
TableName="tblTraders"
OnSelecting="TrustsDataSource_Selecting"/>
With my LinqDataSources OnSelecting event code holding my query...
Code:
protected void TrustsDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
TradeSelectorDataContext db = new TradeSelectorDataContext();
var query =
(
from tt in db.tblTraders
join ttcl in db.tblCityLookups on tt.city_id equals ttcl.city_id
join lctt in db.tblCountryLookups on tt.country_id equals lctt.country_id
join tcl in db.tblCountyLookups on tt.county_id equals tcl.county_id
join twal in db.tblTraderWorkingAreaLookups on tt.trader_working_area_id equals twal.trader_working_area_id
join ttrade in db.tblTraderTrades on tt.UserId equals ttrade.UserId
join trade in db.tblTrades on ttrade.trade_id equals trade.trade_id
join ttt in db.tblTradeTradeTypes on trade.trade_id equals ttt.trade_id
join tradet in db.tblTradeTypes on ttrade.trade_type_id equals tradet.trade_type_id
join cj in db.tblCustomerJobs on trade.trade_id equals cj.trade_id
join tr in db.tblTraderRatings on tt.UserId equals tr.UserId
//join tc in db.tblCustomers on cj.UserId equals tc.UserId
orderby tt.trader_firstname ascending
select new
{
tt.UserId
,
tt.trader_title
,
tt.trader_firstname
,
tt.trader_surname
,
tt.trader_phone1
,
tt.trader_phone2
,
tt.trader_working_area_id
,
tt.trader_overall_rating
,
trader_working_area_name = twal.trader_working_area_name.Substring(0, twal.trader_working_area_name.IndexOf('(')).Trim()
,
trader_working_area_postcodes = twal.trader_working_area_name.Substring(twal.trader_working_area_name.IndexOf('(') + 1, twal.trader_working_area_name.LastIndexOf(')') - twal.trader_working_area_name.IndexOf('(') - 1).Trim()
,
trade_type_name = tradet.trade_type_name == null ? "" : tradet.trade_type_name
,
trade.trade_name
,
ttrade.trade_type_id
,
ttrade.trade_id
,
tr.Rating01
,
tr.Rating02
,
tr.Rating03
,
tr.Rating04
,
tr.Rating05
}
).Distinct();
e.Result = query;
}
Any help appreciated.
|
|
 |