Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #21 (permalink)  
Old April 7th, 2010, 12:07 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

You'll find a link to it in the comments on the original article: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=465

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #22 (permalink)  
Old April 7th, 2010, 01:10 PM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
}
}
}
}
  #23 (permalink)  
Old April 7th, 2010, 01:27 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #24 (permalink)  
Old April 14th, 2010, 12:44 PM
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;
    }
}
  #25 (permalink)  
Old April 14th, 2010, 12:51 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

Excellent. Thanks for the update.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #26 (permalink)  
Old April 14th, 2010, 12:54 PM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
  #27 (permalink)  
Old April 14th, 2010, 01:14 PM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

You should be able to just replace it.
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 14th, 2010 at 02:34 PM.
  #28 (permalink)  
Old April 15th, 2010, 06:41 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
  #29 (permalink)  
Old April 15th, 2010, 07:03 AM
Imar's Avatar
Wrox Author
Points: 71,796, Level: 100
Points: 71,796, Level: 100 Points: 71,796, Level: 100 Points: 71,796, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,036
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

If that query returns objects that look like your test class, this should work.

What is not working? Do you get an error?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #30 (permalink)  
Old April 15th, 2010, 07:58 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate a ASP.Net Treeview control dotnetDeveloper ADO.NET 2 November 21st, 2008 07:25 AM
how to use asp.net we user control on asp 3.0 web i_shahid Java Basics 2 January 8th, 2008 09:04 AM
using asp.net web user control in asp 3.0 App i_shahid Classic ASP Professional 0 January 8th, 2008 07:32 AM
Control in ASP.NET Manisha0605 .NET Framework 2.0 1 March 29th, 2007 03:33 PM
XML use in ASP (pre .NET) Lucky Bastard Classic ASP XML 0 April 8th, 2005 12:08 AM



All times are GMT -4. The time now is 04:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.