MSDN Magazine features a new ASP.NET 2.0 Grid control called GridView.
http://msdn.microsoft.com/msdnmag/issues/04/08/GridView/default.aspx
I have been working with this control in conjunction with an ObjectDataSource in order to display data directly from a domain object. The GridView in combination with the DataObjectSource allows me to pursue an object driven design in my applications.
I initially setup the GridView in design mode with Bound Fields. To add the bound fields, I used the field dialog, accessible by clicking on the expand button on the Columns property of the gridView.
This worked fine so long as the domain object that was linked to the ObjectDataSource was “flat”.
So if I had a class...
public OrderItem
{
private string productName;
public string ProductName
{
get
{
return productName;
}
set
{
productName = value;
}
}
private int orderQuantity;
public int OrderQuantity;
{
get
{
return orderQuantity;
}
set
{
orderQuantity = value;
}
}
private decimal productPrice;
public decimal ProductPrice;
{
get
{
return productPrice;
}
set
{
productPrice = value;
}
}
public OrderItem()
{
productName ="";
orderQuantity = 0;
productPrice = 0.00M;
}
}
I can then set the BoundFields DataField property to the OrderItem object properties ie ProductName, OrderQuantity, or ProductPrice.
But what happens when I have a domain object that is composed of other referenced objects...
public Product
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private decimal price;
public decimal Price;
{
get
{
return price;
}
set
{
price = value;
}
}
public Product()
{
name ="";
price = 0.00M;
}
}
public OrderItem
{
private Product associatedProduct;
public Product AssociatedProduct
{
get
{
return associatedProduct;
}
set
{
associatedProduct= value;
}
}
private int orderQuantity;
public int OrderQuantity;
{
get
{
return orderQuantity;
}
set
{
orderQuantity = value;
}
}
public OrderItem()
{
associatedProduct=null;
orderQuantity = 0;
}
}
Now when I set the BoundFields DataField property to the Products properties via the OrderItems AssociatedProduct property ie AssociatedProduct.Name or AssociatedProduct.Price the bound field displays a nothing.
In order to get this to work I had to use a TemplateField instead of a BoundField. The field dialog has a nifty link button that can be used to convert the BoundField to a TemplateField.
All I had to do was create a bound field with a DataField property of AssociatedProduct.Name and then press the “Convert this field into a Template Field“ button and presto the product name was now displaying.
Magic I say..
I've recently been having fun trying to get the type of a generic list using reflection.
The VS2005 MSDN doco suggests trying something like..
Type classType = Type.GetType(“System.Collections.Generic.List[[System.String,Mscorlib]],Mscorlib“,true);
I kept getting a a TypeLoadException : Could not load type 'System.Collections.Generic.List' from assembly mscorlib.
After a bit of deliberating, I decided to try the following..
System.Collections.Generic.List<String> stringList = new System.Collections.Generic.List<String>();
Type classType = stringList.GetType();
Console.WriteLine(classType.ToString());
Guess what I got...
System.Collections.Generic.List`1[System.String]
So what is this `1 about, it seems to be added to generic classes.
From a bit more investigation it seems to represent the number of generic parameters (dimensions) associated with the class, so Dictionary is Dictionary`2. While a generic class like MyGeneric<A,B,C,D,E> is actual MyGeneric`5.
I have been getting an argument exception with details “Absolute path information required”, this exception was happening when I was running Tests with Nunit.
After almost pulling my hair out the problem was found, Environment variables....
Nunit caches the assemblies that are being tested in the temp directory, and uses the TEMP environment variable. Of course my TEMP environment variable was a relative path.
So the moral is, if you see Absolute path information is required take a look at your environment variables and ensure that they are absolute.
More on getting Nunitasp working with VS2005. I have been having a problem with the LinkButtonTester, and it turns out that ASP.NET 2.0 doesn't like having “NunitAsp” as the Http Request UserAgent setting. When I set the UserAgent to be Mozilla/4.0 the page was rendered correctly.
So if you are have problems with LinkButtonTester make sure you include the following in your test or test fixture setup..
Browser.UserAgent = "Mozilla/4.0";
I have recently encountered another issue with the LinkButtonTester. I had a page with a LinkButton, TextBox, and Field Validator. The rendered page used a different javascript function called
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(“test“,““,true,))
The LinkButtonTester didn't like this at all raising a parse exception.
It turns out that in ASP.NET 2.0 validation is also performed client side using various javascripts, and when a linkbutton is clicked it triggers the client side validation to occur.
So in order to turn off this behaviour and return to the old __doPostBack function I set the LinkButton's CausesValidation property to false.
I have recently been investigating a exception raised when I call the click method of the LinkButtonTester within NunitASP.
This wasn working fine with ASP.NET 1.1 but is now broken with ASP.NET 2.0.
I debugged the operation of Nunit to see what was causing the exception and used a simple scenario with a aspx page with a link button.
It turns out that the server was not emitting the __EVENTTARGET and __EVENTARGUMENT fields or the __doPostBack script, even though the anchor was still referencing the script.
When I call the page via Internet Explorer, the page was emitting correctly.
I can only assume that ASP.NET 2.0 is expecting something in the HTTP Request that NunitAsp is not currently providing.
Does anyone have an ideas on this?
I was lucky enough to have the opportunity to conduct a presentation on CVS at the Melbourne Dot net user group.
This experience has given me alot of valuable experience, I have decided to keep jumping in head first a get involved in more of this community activity.
If I had the opportunity to another presentation on CVS, I would start with a clean system and demonstrate how easy it is to setup a CVS server, Client, and then add a repository.
The other thing I found was that my knowledge of CVS was increased from audience questions and comments.
My first post to this great blog framework (thanks Scott for all the effort behind this) is really to get that instant gratification of getting something working.
More Posts