 |
| 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, 06:27 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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);
}
}
|
|

April 7th, 2010, 06:32 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 7th, 2010, 06:41 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 7th, 2010, 06:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 7th, 2010, 06:52 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

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

April 7th, 2010, 09:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

April 7th, 2010, 10:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

April 7th, 2010, 10:37 AM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Please forward me a link to your repeater example and I will have a look.
Thanks.
|
|
 |