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
| 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 Search this Thread Display Modes
  #11 (permalink)  
Old April 7th, 2010, 06:27 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default default.cs page

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;

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");
        }

        BindData();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        //BindData();
    }

    private void BindData()
    {
        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();
    }

    protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        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 
        };

        // Get a reference to the rating control, and set and bind the data
        Spaanjaars.Toolkit.ContentRating ratingControl = (Spaanjaars.Toolkit.ContentRating)e.Item.FindControl("ContentRating1");

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ratingControl.ItemId = userid;
            ratingControl.DataSource = rateValues;
            ratingControl.DataBind();
        }
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetCompletionList(string contextKey, string prefixText, int count)
    {
        //  prefixText: the text the user has entered thus far
        //  count: the number of suggestions to return
        //  contextKey: the column name to fetch suggestions for           
        return new SelectCustomers().GetCompletionList(contextKey, prefixText, count);
    }
}
  #12 (permalink)  
Old April 7th, 2010, 06:32 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Above I have attached cut down versions of my default.aspx and default.cs pages. I have left my linq datasource in there as I feel this is a simple as it can get and I really need to understand a solution that caters for a database.

Thanks.
  #13 (permalink)  
Old April 7th, 2010, 06:41 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
This is a bit too much code for me to read and digest. Also, there are way too many dependencies (AJAX, Update Panels and so on). Can you try reproducing this in a simple scenario with, preferably a fake data source that doesn't rely on a database.
Not sure I understand what problem this new code is trying to solve....

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!
  #14 (permalink)  
Old April 7th, 2010, 06:50 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I really need to understand a solution that caters for a database.
Because of the database dependency, I cannot run this code, and thus I cannot see what the problem is.

You don't *need* a database; you need a data source to understand how this works. Why not create a simple List<Whatever> where Whatever has a Name, a Rating and what have you and bind the source using an ObjectDataSource? This way, you remove the dependency on the database and create a scenario that's easily understood and reproducible...

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!
  #15 (permalink)  
Old April 7th, 2010, 06:52 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I can't seem to bind my database column "UserId" (returned from my linq query within my BindData method) to your rating control's ItemId attribute?
  #16 (permalink)  
Old April 7th, 2010, 09:11 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Due to using an asp.net listview control I can't seem to find your control within my listview's ItemTemplate? I have tried the below code within my Page_Load event but I receive the error "object reference not set to an instance of an object"?

Spaanjaars.Toolkit.ContentRating ratingControl = (Spaanjaars.Toolkit.ContentRating)lvTrustAccounts. FindControl("ContentRating1");

Thanks.
  #17 (permalink)  
Old April 7th, 2010, 09:26 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Did you try e.Item.FindControl?

If you'd posted a reproducible scenario, I could help you much better and we could have resolved this days ago. Now, it's just too difficult for me to see what you're doing without spending a lot of time going through (unused) code.

Did you see my example using a Repeater?

And what are you using Reflection? Can't you simply cast the data item?

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!
  #18 (permalink)  
Old April 7th, 2010, 10:14 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Within my page_load event I don't get the option to choose e.Item? I am using: protected void Page_Load(object sender, EventArgs e)....

I am trying to build a reproducable scenario for you by using your articles ViewState approach but unfortunately I can't seem to find a way to bind your rating control (found within the ItemTemplate of my listview control) attributes within my code behinds page_load event?

i.e.

protected void Page_Load(object sender, EventArgs e)
{
Spaanjaars.Toolkit.ContentRating ratingControl = (Spaanjaars.Toolkit.ContentRating)lvTrustAccounts. FindControl("ContentRating1");

if (!IsPostBack)
{
ratingControl.ItemId = Guid.NewGuid(); (ERROR flagged on this line)
ratingControl.DataSource = Values;
ratingControl.DataBind();
}
}

The above flags the error I previously mentioned: Object reference not set to an instance of an object.

Last edited by martinspalding; April 7th, 2010 at 10:17 AM..
  #19 (permalink)  
Old April 7th, 2010, 10:27 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I am trying to build a reproducable scenario for you by using your articles ViewState approach
I wouldn't do that. First of all, that is already a working example, but more importantly, it's not what you're after. What's different in your case is that you're having multiple items in a ListView. You're on the right track by using the events fired by the ListView, but you need to use e.Item.FindControl there.

Again, take a look at my Repeater example (don't know if you already have as you didn't answer that question). It shows you a complete working example with a simple data source as I explained earlier and an ObjectDataSource. Copy and paste, and you should be good to.

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!
  #20 (permalink)  
Old April 7th, 2010, 10:37 AM
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please forward me a link to your repeater example and I will have a look.

Thanks.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 02:33 PM
XML use in ASP (pre .NET) Lucky Bastard Classic ASP XML 0 April 7th, 2005 11:08 PM



All times are GMT -4. The time now is 07:24 AM.


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